UnitHistoryService.php 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. namespace App\Services;
  3. use App\Models\UnitHistory;
  4. use Illuminate\Database\Eloquent\Collection;
  5. class UnitHistoryService
  6. {
  7. public function findById(int $id): ?UnitHistory
  8. {
  9. return UnitHistory::find($id);
  10. }
  11. public function create(array $data): UnitHistory
  12. {
  13. return UnitHistory::create($data);
  14. }
  15. public function update(int $id, array $data): ?UnitHistory
  16. {
  17. $model = $this->findById($id);
  18. if (!$model) {
  19. return null;
  20. }
  21. $model->update($data);
  22. return $model->fresh();
  23. }
  24. public function delete(int $id): bool
  25. {
  26. $model = $this->findById($id);
  27. if (!$model) {
  28. return false;
  29. }
  30. return $model->delete();
  31. }
  32. //
  33. public function getByUnitId(int $unitId): Collection
  34. {
  35. return UnitHistory::where('unit_id', $unitId)
  36. ->orderBy('created_at', 'desc')
  37. ->get();
  38. }
  39. public function getVisibleByUnitId(int $unitId): Collection
  40. {
  41. return UnitHistory::where('unit_id', $unitId)
  42. ->where('visible_to_franchisee', true)
  43. ->orderBy('created_at', 'desc')
  44. ->get();
  45. }
  46. }