35 lines
1.0 KiB
PHP
35 lines
1.0 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Repositories;
|
|
|
|
use App\Models\ClientGroup;
|
|
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
|
|
|
|
class ClientGroupRepository extends BaseRepository implements ClientGroupRepositoryInterface
|
|
{
|
|
public function __construct(ClientGroup $model)
|
|
{
|
|
parent::__construct($model);
|
|
}
|
|
|
|
public function paginate(int $perPage = 15, array $filters = []): LengthAwarePaginator
|
|
{
|
|
$query = $this->model->newQuery()->withCount('clients');
|
|
|
|
if (!empty($filters['search'])) {
|
|
$query->where(function ($builder) use ($filters) {
|
|
$builder
|
|
->where('name', 'like', '%' . $filters['search'] . '%')
|
|
->orWhere('description', 'like', '%' . $filters['search'] . '%');
|
|
});
|
|
}
|
|
|
|
$sortField = $filters['sort_by'] ?? 'created_at';
|
|
$sortDirection = $filters['sort_direction'] ?? 'desc';
|
|
|
|
return $query->orderBy($sortField, $sortDirection)->paginate($perPage);
|
|
}
|
|
}
|