| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- <?php
- namespace App\Services;
- use App\Models\City;
- use App\Models\State;
- use Illuminate\Database\Eloquent\Collection;
- class CityService
- {
- public function getAll(): Collection
- {
- return City::query()->orderBy("name", "asc")->get();
- }
- public function findById(int $id): ?City
- {
- return City::find($id);
- }
- public function create(array $data): City
- {
- return City::create($data);
- }
- public function update(int $id, array $data): ?City
- {
- $model = $this->findById($id);
- if (!$model) {
- return null;
- }
- $model->update($data);
- return $model->fresh();
- }
- public function delete(int $id): bool
- {
- $model = $this->findById($id);
- if (!$model) {
- return false;
- }
- return $model->delete();
- }
- public function findStateCityByDescription(string $state, string $city): ?array
- {
- $state = State::query()
- ->where('code', $state)
- ->select(
- 'name as label',
- 'id as value',
- )
- ->first();
- $city = City::query()
- ->where('state_id', $state->value)
- ->where('name', $city)
- ->select(
- 'name as label',
- 'id as value',
- 'state_id',
- )
- ->first();
- return [
- 'state' => $state,
- 'city' => $city
- ];
- }
- }
|