Country.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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 Webkul\BagistoApi\Resolver\BaseQueryItemResolver;
  10. use Webkul\BagistoApi\State\CursorAwareCollectionProvider;
  11. use Webkul\Core\Models\Country as BaseCountry;
  12. #[ApiResource(
  13. routePrefix: '/api/shop',
  14. paginationEnabled: false,
  15. operations: [
  16. new GetCollection,
  17. new Get,
  18. ],
  19. graphQlOperations: [
  20. new Query(resolver: BaseQueryItemResolver::class),
  21. new QueryCollection(provider: CursorAwareCollectionProvider::class, paginationEnabled: false),
  22. ]
  23. )]
  24. class Country extends BaseCountry
  25. {
  26. #[ApiProperty(readableLink: true)]
  27. public function getStates()
  28. {
  29. return $this->states;
  30. }
  31. public function getNameAttribute($value)
  32. {
  33. return $value;
  34. }
  35. public function states()
  36. {
  37. return $this->hasMany(\Webkul\BagistoApi\Models\CountryState::class, 'country_id');
  38. }
  39. /**
  40. * Get the default translation for the country
  41. * Note: This field is excluded from GraphQL to avoid null conflicts.
  42. * Use 'translations' (collection) instead to get all translations.
  43. */
  44. public function getTranslation(?string $locale = null, ?bool $withFallback = null): ?\Illuminate\Database\Eloquent\Model
  45. {
  46. return parent::getTranslation($locale, $withFallback);
  47. }
  48. #[ApiProperty(readableLink: true, description: 'Translations for the country')]
  49. public function getTranslations()
  50. {
  51. return $this->getAttribute('translations') ?? parent::translations();
  52. }
  53. }