| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 |
- <?php
- namespace App\Repositories;
- use App\Models\State;
- use App\DTO\StateDTO;
- use Illuminate\Database\Eloquent\Collection;
- class StateRepository implements StateRepositoryInterface
- {
- public function __construct(
- protected State $model
- ){
- }
- public function all(): Collection
- {
- return $this->model->with('country')->get();
- }
- public function find(int $id): ?State
- {
- return $this->model->with('country')->find($id);
- }
- public function create(StateDTO $dto): State
- {
- return $this->model->create($dto->toArray());
- }
- public function update(int $id, StateDTO $dto, array $fieldsToUpdate): State
- {
- $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;
- }
- }
|