Hi folks,
I'm having problems with filtering related objects:
2 Tables: customers
and bills
A customer hasMany
bills.
/* Customers class */
public function bills() { return $this->hasMany('bills'); }
In my controller I want to filter the bills of a customer. But how? I tried this:
$customers = Customer::all();
$customers = $customers->filter(function($customer) {
$customer->bills->filter(function($bill) {
return false;
});
return true;
});
Expected result was all customers, but no bills. But it didn't work :-(...
Any ideas? Thanks
Found the solution:
$customers = Customer::all();
$customers->each(function($customer) {
$customer->bills = $customer->bills->filter(function($bill) {
if($bill->foo == 'bar')
return true;
return false;
})
});
Hmm... I think it would be better to do the filtering within the model (either customers or bills, idk yet). But don't can figure out how to do that. Any ideas?
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community