| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 |
- <?php
- namespace App\Repositories;
- use App\Models\City;
- use App\DTO\CityDTO;
- use Illuminate\Database\Eloquent\Collection;
- class CityRepository implements CityRepositoryInterface
- {
- public function __construct(
- protected City $model
- ) {}
- public function all(): Collection
- {
- return $this->model->with('state', 'country')->get();
- }
- public function find(int $id): ?City
- {
- return $this->model->with('state', 'country')->find($id);
- }
- public function create(CityDTO $dto): City
- {
- return $this->model->create($dto->toArray());
- }
- public function update(int $id, CityDTO $dto, array $fieldsToUpdate): City
- {
- $record = $this->find($id);
- $updateFields = array_intersect_key(
- $dto->toArray(),
- array_flip($fieldsToUpdate)
- );
- $record->update($updateFields);
- return $record->fresh();
- }
- public function delete(int $id): bool
- {
- return $this->model->destroy($id) > 0;
- }
- }
|