To achieve the desired functionality, you can modify your filtering logic to handle the special case when the user selects the "unpaid" status. You can use the whereIn method to filter invoices with statuses of "waiting", "pending", or "attested". Here's how you can adjust your render method:
public function render(): View
{
$invoices = Invoice::query()
->select(['invoices.*']);
foreach ($this->searchColumns as $column => $value) {
if (!empty($value)) {
$invoices
->when($column === 'ub', fn($invoices) => $invoices->whereRelation('user', 'users.id', $value))
->when($column === 'status', function ($invoices) use ($value) {
if ($value === 'unpaid') {
// If the status is 'unpaid', filter for 'waiting', 'pending', or 'attested'
$invoices->whereIn('invoices.status', ['waiting', 'pending', 'attested']);
} else {
// Otherwise, perform a normal LIKE search
$invoices->where('invoices.status', 'LIKE', '%' . $value . '%');
}
});
}
}
$invoices->orderBy($this->sortColumn, $this->sortDirection);
return view('livewire.invoices.all', [
'invoices' => $invoices->simplePaginate(10),
]);
}
Explanation:
-
Check for 'unpaid' Status: In the
whenclause for thestatuscolumn, check if the$valueis 'unpaid'. -
Use
whereIn: If the status is 'unpaid', use thewhereInmethod to filter invoices with statuses 'waiting', 'pending', or 'attested'. -
Default Behavior: If the status is not 'unpaid', continue with the existing
LIKEsearch logic.
This approach allows you to add 'unpaid' to your $statuses array for the dropdown, and when selected, it will correctly filter the invoices based on the specified statuses.