CountryService.php 920 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. <?php
  2. namespace App\Services;
  3. use App\Models\Country;
  4. use Illuminate\Database\Eloquent\Collection;
  5. class CountryService
  6. {
  7. public function getAll(): Collection
  8. {
  9. return Country::query()->orderBy('created_at', 'desc')->get();
  10. }
  11. public function findById(int $id): ?Country
  12. {
  13. return Country::find($id);
  14. }
  15. public function create(array $data): Country
  16. {
  17. return Country::create($data);
  18. }
  19. public function update(int $id, array $data): ?Country
  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. }