Service.stub 991 B

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