dstpabuaran.com/app/Services/Admin/Master/CategoryService.php

68 lines
1.7 KiB
PHP

<?php
namespace App\Services\Admin\Master;
use App\Models\Category;
use App\Services\Concerns\LogsFormHistory;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Illuminate\Database\Eloquent\Collection;
class CategoryService
{
use LogsFormHistory;
public function paginated(int $perPage = 25, string $search = '', string $sort = 'created_at', string $direction = 'desc', array $filters = []): LengthAwarePaginator
{
return Category::query()
->select(['id', 'name'])
->when($search, fn ($q) => $q->where('name', 'like', "%{$search}%"))
->orderBy($sort, $direction)
->paginate($perPage);
}
public function getAll(): Collection
{
return Category::select(['id', 'name'])->latest()->get();
}
public function store(array $data): Category
{
$category = Category::create($data);
$this->logCreated(
model: $category,
module: 'Kategori',
newValues: ['Nama' => $category->name],
);
return $category;
}
public function update(Category $category, array $data): Category
{
$oldValues = ['Nama' => $category->name];
$category->update($data);
$this->logUpdated(
model: $category,
module: 'Kategori',
oldValues: $oldValues,
newValues: ['Nama' => $category->name],
);
return $category;
}
public function destroy(Category $category): bool
{
$this->logDeleted(
model: $category,
module: 'Kategori',
oldValues: ['Nama' => $category->name],
);
return $category->delete();
}
}