Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

City.php 1.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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 string $country_id
  12. * @property float $latitude
  13. * @property float $longitude
  14. */
  15. class City extends Model
  16. {
  17. public static array $schema = [
  18. 'id' => 'integer',
  19. 'name' => 'string',
  20. 'state_id' => 'integer',
  21. 'state_code' => 'string',
  22. 'country_id' => 'string',
  23. 'latitude' => 'float',
  24. 'longitude' => 'float',
  25. ];
  26. public static function getCitiesByCountryAndState(?string $countryCode, ?string $stateId): Collection
  27. {
  28. if ($stateId === null || $countryCode === null) {
  29. return collect();
  30. }
  31. return self::query()->where('country_id', $countryCode)
  32. ->where('state_id', $stateId)
  33. ->get();
  34. }
  35. public static function getCityOptions(?string $countryCode = null, ?string $stateId = null): Collection
  36. {
  37. if ($countryCode === null || $stateId === null) {
  38. return collect();
  39. }
  40. return self::getCitiesByCountryAndState($countryCode, $stateId)->pluck('name', 'id');
  41. }
  42. public function state(): BelongsTo
  43. {
  44. return $this->belongsTo(State::class, 'state_id', 'id');
  45. }
  46. public function country(): BelongsTo
  47. {
  48. return $this->belongsTo(Country::class, 'country_id', 'id');
  49. }
  50. }