| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 |
- <?php
- namespace App\Services;
- use App\Models\Category;
- use Illuminate\Database\Eloquent\Collection;
- class CategoryService
- {
- public function getAll(?string $type = null): Collection
- {
- return Category::when($type, fn($q) => $q->where('type', $type))
- ->orderBy('name')
- ->get();
- }
- public function findById(int $id): ?Category
- {
- return Category::find($id);
- }
- public function create(array $data): Category
- {
- return Category::create($data);
- }
- public function update(int $id, array $data): ?Category
- {
- $model = $this->findById($id);
- if (!$model) {
- return null;
- }
- $model->update($data);
- return $model->fresh();
- }
- public function delete(int $id): bool
- {
- $model = $this->findById($id);
- if (!$model) {
- return false;
- }
- return $model->delete();
- }
- }
|