1 | <?php |
||
2 | |||
3 | namespace UKTowns\Models; |
||
4 | |||
5 | use Illuminate\Database\Eloquent\Builder; |
||
6 | use Illuminate\Database\Eloquent\Casts\Attribute; |
||
7 | use Illuminate\Database\Eloquent\Factories\HasFactory; |
||
8 | use Illuminate\Database\Eloquent\Model; |
||
9 | |||
10 | class GeoTown extends Model |
||
11 | { |
||
12 | use HasFactory, HasCoordinates; |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
13 | |||
14 | protected $guarded = []; |
||
15 | |||
16 | 2 | public function getTable(): string |
|
17 | { |
||
18 | 2 | return config('uk-towns.tables.towns'); |
|
19 | } |
||
20 | |||
21 | protected static function newFactory(): GeoTownFactory |
||
22 | { |
||
23 | return GeoTownFactory::new(); |
||
24 | } |
||
25 | |||
26 | public function name(): Attribute |
||
27 | { |
||
28 | return Attribute::get(fn () => implode(', ', array_filter([ |
||
29 | $this->place_name, |
||
0 ignored issues
–
show
|
|||
30 | $this->county, |
||
0 ignored issues
–
show
|
|||
31 | $this->region, |
||
0 ignored issues
–
show
|
|||
32 | ]))); |
||
33 | } |
||
34 | |||
35 | 1 | public static function countryOptions(): array |
|
36 | { |
||
37 | 1 | $column = 'country'; |
|
38 | 1 | $countries = static::query() |
|
39 | 1 | ->groupBy($column) |
|
40 | 1 | ->orderBy($column) |
|
41 | 1 | ->select($column) |
|
42 | 1 | ->get(); |
|
43 | |||
44 | 1 | return $countries->map(fn ($region) => [ |
|
45 | 1 | 'label' => $region->$column, |
|
46 | 1 | 'value' => $region->$column, |
|
47 | 1 | ])->toArray(); |
|
48 | } |
||
49 | |||
50 | 1 | public static function regionOptions(?string $country = null): array |
|
51 | { |
||
52 | 1 | $column = 'region'; |
|
53 | 1 | $regions = static::query() |
|
54 | 1 | ->when($country, fn (Builder $q) => $q->where('country', $country)) |
|
55 | 1 | ->groupBy($column) |
|
56 | 1 | ->orderBy($column) |
|
57 | 1 | ->select($column) |
|
58 | 1 | ->get(); |
|
59 | |||
60 | 1 | return $regions->map(fn ($region) => [ |
|
61 | 1 | 'label' => $region->$column, |
|
62 | 1 | 'value' => $region->$column, |
|
63 | 1 | ])->toArray(); |
|
64 | } |
||
65 | |||
66 | 1 | public static function countyOptions(?string $region = null): array |
|
67 | { |
||
68 | 1 | $column = 'county'; |
|
69 | 1 | $items = static::query() |
|
70 | 1 | ->when($region, fn (Builder $q) => $q->where('region', $region)) |
|
71 | 1 | ->groupBy($column) |
|
72 | 1 | ->orderBy($column) |
|
73 | 1 | ->select($column) |
|
74 | 1 | ->get(); |
|
75 | |||
76 | 1 | return $items->map(fn ($region) => [ |
|
77 | 1 | 'label' => $region->$column, |
|
78 | 1 | 'value' => $region->$column, |
|
79 | 1 | ])->toArray(); |
|
80 | } |
||
81 | |||
82 | 1 | public static function townOptions(string $county): array |
|
83 | { |
||
84 | 1 | $column = 'place_name'; |
|
85 | 1 | $items = static::query() |
|
86 | 1 | ->where('county', $county) |
|
87 | 1 | ->groupBy($column) |
|
88 | 1 | ->orderBy($column) |
|
89 | 1 | ->select($column) |
|
90 | 1 | ->get(); |
|
91 | |||
92 | 1 | return $items->map(fn ($region) => [ |
|
93 | 1 | 'label' => $region->$column, |
|
94 | 1 | 'value' => $region->$column, |
|
95 | 1 | ])->toArray(); |
|
96 | } |
||
97 | } |
||
98 |