I am in trouble with a little functionnality I want to add in my project, sure someone can help me :)
The stack is Laravel with Livewire to add easily some interactivity.
I have a table with a list of bills. I use Livewire pagination in this table, and I have a search field to dynamically filter those bills. There's no problem for a simple filter, for example by bill numerotation, or by date, or both... using where and orWhere clauses. I have an additionnal filter, activated by a button, which filters bills by "paid or not". For this case, my Livewire Controller should look like this :
public function render()
{
return view('livewire.bills.index', [
'bills' => Bill::where('numero', 'like', '%'.$this->search.'%') // string modified by search input
->orWhere('created_at', 'like', '%'.$this->search.'%') // string modified by search input
->where('paid', $this->paid) // bool switched by click on a button
->paginate(12),
]);
}
In addition, I want to allow filter by customer name. Of course, Bill Model and Customer Model are relationned in a One-To-many Relationship :
-- Bill --
-id
-numero (string)
-customer_id (foreign)
-paid (bool)
-price(int)
-created_at (datetime)
-- Customer --
-id
-name (string)
To allow filter by customer name and 'paid or not', i use whereHas clause in addition to the where clause :
'bills' => Bill::whereHas('customer', function($query) {
return $query->where('name', 'like', '%'.$this->search.'%');
})
->where('paid', $this->paid)
->paginate(12),
Everything's ok. But I can't add the third filter (filter by created_at) in addition of those 2 filters. I tried this :
return view('livewire.devis.index', [
'bills' => Bill::whereHas('customer', function($query) {
return $query->where('name', 'like', '%' . $this->search . '%');
})
->orWhere('created_at', 'like', '%' . $this->search . '%')
->where('paid', $this->paid)
->paginate(12),
]);
It works for filtering by customer name or by date (search field), but the 'paid or not' filter doesn't work anymore.
Anyone can help me to understant where my query is failing please.
Thanks to everybody who's watching this and maybe could give me a hand