87 lines
2.3 KiB
PHP
87 lines
2.3 KiB
PHP
<?php
|
|
|
|
namespace App\Repositories;
|
|
|
|
use App\Models\Deceased;
|
|
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
|
|
use Illuminate\Database\Eloquent\Collection;
|
|
|
|
class DeceasedRepository extends BaseRepository implements DeceasedRepositoryInterface
|
|
{
|
|
/**
|
|
* DeceasedRepository constructor.
|
|
*/
|
|
public function __construct(Deceased $model)
|
|
{
|
|
parent::__construct($model);
|
|
}
|
|
|
|
/**
|
|
* Get all deceased with optional filtering and pagination
|
|
*
|
|
* @param array $filters
|
|
* @param int $perPage
|
|
* @return LengthAwarePaginator
|
|
*/
|
|
public function getAllPaginated(array $filters = [], int $perPage = 15): LengthAwarePaginator
|
|
{
|
|
$query = $this->model->newQuery();
|
|
|
|
// Apply filters
|
|
if (!empty($filters['search'])) {
|
|
$query->where(function($q) use ($filters) {
|
|
$q->where('last_name', 'LIKE', "%{$filters['search']}%")
|
|
->orWhere('first_name', 'LIKE', "%{$filters['search']}%");
|
|
});
|
|
}
|
|
|
|
// Apply date range filters
|
|
if (!empty($filters['start_date'])) {
|
|
$query->where('death_date', '>=', $filters['start_date']);
|
|
}
|
|
|
|
if (!empty($filters['end_date'])) {
|
|
$query->where('death_date', '<=', $filters['end_date']);
|
|
}
|
|
|
|
// Apply sorting
|
|
$sortBy = $filters['sort_by'] ?? 'created_at';
|
|
$sortOrder = $filters['sort_order'] ?? 'desc';
|
|
$query->orderBy($sortBy, $sortOrder);
|
|
|
|
// Eager load related counts
|
|
$query->withCount(['documents', 'interventions']);
|
|
|
|
return $query->paginate($perPage);
|
|
}
|
|
|
|
/**
|
|
* Find a deceased by ID
|
|
*
|
|
* @param int $id
|
|
* @return Deceased
|
|
*/
|
|
public function findById(int $id): Deceased
|
|
{
|
|
return $this->find($id);
|
|
}
|
|
|
|
/**
|
|
* Search deceased by name
|
|
*
|
|
* @param string $name
|
|
* @return Collection
|
|
*/
|
|
public function searchByName(string $name): Collection
|
|
{
|
|
return $this->model->newQuery()
|
|
->where(function($query) use ($name) {
|
|
$query->where('last_name', 'LIKE', "%{$name}%")
|
|
->orWhere('first_name', 'LIKE', "%{$name}%");
|
|
})
|
|
->orderBy('last_name', 'asc')
|
|
->orderBy('first_name', 'asc')
|
|
->get();
|
|
}
|
|
}
|