MediaService.php 933 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. <?php
  2. namespace App\Services;
  3. use App\Models\Media;
  4. use Illuminate\Database\Eloquent\Collection;
  5. class MediaService
  6. {
  7. public function getAll(): Collection
  8. {
  9. return Media::query()
  10. ->with(['user'])
  11. ->orderBy("created_at", "desc")
  12. ->get();
  13. }
  14. public function findById(int $id): ?Media
  15. {
  16. return Media::with(['user'])->find($id);
  17. }
  18. public function create(array $data): Media
  19. {
  20. return Media::create($data);
  21. }
  22. public function update(int $id, array $data): ?Media
  23. {
  24. $model = $this->findById($id);
  25. if (!$model) {
  26. return null;
  27. }
  28. $model->update($data);
  29. return $model->fresh(['user']);
  30. }
  31. public function delete(int $id): bool
  32. {
  33. $model = $this->findById($id);
  34. if (!$model) {
  35. return false;
  36. }
  37. return $model->delete();
  38. }
  39. }