Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

City.php 1.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. namespace App\Models\Locale;
  3. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  4. use Illuminate\Support\Collection;
  5. use Squire\Model;
  6. /**
  7. * @property int $id
  8. * @property string $name
  9. * @property int $state_id
  10. * @property string $state_code
  11. * @property int $country_id
  12. * @property string $country_code
  13. * @property float $latitude
  14. * @property float $longitude
  15. */
  16. class City extends Model
  17. {
  18. public static array $schema = [
  19. 'id' => 'integer',
  20. 'name' => 'string',
  21. 'state_id' => 'integer',
  22. 'state_code' => 'string',
  23. 'country_id' => 'integer',
  24. 'country_code' => 'string',
  25. 'latitude' => 'float',
  26. 'longitude' => 'float',
  27. ];
  28. public static function getCitiesByCountryAndState(?string $countryCode, ?string $stateId): Collection
  29. {
  30. if ($stateId === null || $countryCode === null) {
  31. return collect();
  32. }
  33. return self::query()->where('country_code', $countryCode)
  34. ->where('state_id', $stateId)
  35. ->get();
  36. }
  37. public static function getCityOptions(?string $countryCode = null, ?string $stateId = null): Collection
  38. {
  39. if ($countryCode === null || $stateId === null) {
  40. return collect();
  41. }
  42. return self::getCitiesByCountryAndState($countryCode, $stateId)->pluck('name', 'id');
  43. }
  44. public function state(): BelongsTo
  45. {
  46. return $this->belongsTo(State::class, 'state_id', 'id');
  47. }
  48. public function country(): BelongsTo
  49. {
  50. return $this->belongsTo(Country::class, 'country_id', 'id');
  51. }
  52. }