您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

State.php 1.2KB

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