StateRepository.php 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. namespace App\Repositories;
  3. use App\Models\State;
  4. use App\DTO\StateDTO;
  5. use Illuminate\Database\Eloquent\Collection;
  6. class StateRepository implements StateRepositoryInterface
  7. {
  8. public function __construct(
  9. protected State $model
  10. ){
  11. }
  12. public function all(): Collection
  13. {
  14. return $this->model->with('country')->get();
  15. }
  16. public function find(int $id): ?State
  17. {
  18. return $this->model->with('country')->find($id);
  19. }
  20. public function create(StateDTO $dto): State
  21. {
  22. return $this->model->create($dto->toArray());
  23. }
  24. public function update(int $id, StateDTO $dto, array $fieldsToUpdate): State
  25. {
  26. $record = $this->find($id);
  27. $updateFields = array_intersect_key(
  28. $dto->toArray(),
  29. array_flip($fieldsToUpdate)
  30. );
  31. $record->update($updateFields);
  32. return $record->fresh();
  33. }
  34. public function delete(int $id): bool
  35. {
  36. return $this->model->destroy($id) > 0;
  37. }
  38. }