StateService.php 894 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. namespace App\Services;
  3. use App\Models\State;
  4. use Illuminate\Database\Eloquent\Collection;
  5. class StateService
  6. {
  7. public function getAll(): Collection
  8. {
  9. return State::query()->orderBy("name", "asc")->get();
  10. }
  11. public function findById(int $id): ?State
  12. {
  13. return State::find($id);
  14. }
  15. public function create(array $data): State
  16. {
  17. return State::create($data);
  18. }
  19. public function update(int $id, array $data): ?State
  20. {
  21. $model = $this->findById($id);
  22. if (!$model) {
  23. return null;
  24. }
  25. $model->update($data);
  26. return $model->fresh();
  27. }
  28. public function delete(int $id): bool
  29. {
  30. $model = $this->findById($id);
  31. if (!$model) {
  32. return false;
  33. }
  34. return $model->delete();
  35. }
  36. // Add custom business logic methods here
  37. }