Channel.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. namespace Webkul\BagistoApi\Models;
  3. use ApiPlatform\Metadata\ApiProperty;
  4. use ApiPlatform\Metadata\ApiResource;
  5. use ApiPlatform\Metadata\Get;
  6. use ApiPlatform\Metadata\GetCollection;
  7. use ApiPlatform\Metadata\GraphQl\Query;
  8. use ApiPlatform\Metadata\GraphQl\QueryCollection;
  9. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  10. use Illuminate\Database\Eloquent\Relations\BelongsToMany;
  11. use Webkul\BagistoApi\Resolver\BaseQueryItemResolver;
  12. use Webkul\BagistoApi\State\ChannelProvider;
  13. #[ApiResource(
  14. routePrefix: '/api/shop',
  15. normalizationContext: ['skip_null_values' => false],
  16. operations: [
  17. new Get(provider: ChannelProvider::class),
  18. new GetCollection(provider: ChannelProvider::class, paginationClientItemsPerPage: true),
  19. ],
  20. graphQlOperations: [
  21. new Query(resolver: BaseQueryItemResolver::class),
  22. new QueryCollection(provider: ChannelProvider::class),
  23. ]
  24. )]
  25. class Channel extends \Webkul\Core\Models\Channel
  26. {
  27. /**
  28. * API Platform identifier
  29. */
  30. #[ApiProperty(identifier: true, writable: false)]
  31. public function getId(): int
  32. {
  33. return $this->id;
  34. }
  35. /**
  36. * Override locales relationship to return API resource model
  37. */
  38. public function locales(): BelongsToMany
  39. {
  40. return $this->belongsToMany(Locale::class, 'channel_locales', 'channel_id', 'locale_id');
  41. }
  42. /**
  43. * Override currencies relationship to return API resource model
  44. */
  45. public function currencies(): BelongsToMany
  46. {
  47. return $this->belongsToMany(Currency::class, 'channel_currencies', 'channel_id', 'currency_id');
  48. }
  49. /**
  50. * Override default locale relationship to return API resource model
  51. */
  52. public function default_locale(): BelongsTo
  53. {
  54. return $this->belongsTo(Locale::class);
  55. }
  56. /**
  57. * Override base currency relationship to return API resource model
  58. */
  59. public function base_currency(): BelongsTo
  60. {
  61. return $this->belongsTo(Currency::class);
  62. }
  63. /**
  64. * Expose logo URL for API
  65. */
  66. #[ApiProperty(writable: false, readable: true)]
  67. public function getLogoUrl(): ?string
  68. {
  69. return $this->logo_url();
  70. }
  71. /**
  72. * Expose favicon URL for API
  73. */
  74. #[ApiProperty(writable: false, readable: true)]
  75. public function getFaviconUrl(): ?string
  76. {
  77. return $this->favicon_url();
  78. }
  79. }