Reputation: 67
i have a question about the code above , i want to search with criteria from 2 extra tables I want 'charge' from charges table , 'name' from user table and also 'name' from customer table all has binds the above query runs but the doesnt fetch data from customers->name any idea how to make it work?
public function scopeSearch($query, $val){
return $query->has('customer')
->whereHas('user', function($query) use ($val) {
$query->where('tasks','like','%'.$val.'%')
->Orwhere('name','like','%'.$val.'%');
})
->with('user')
->with('customer');
}
Upvotes: 0
Views: 274
Reputation: 67
Finally worked! after some research i found that the problem was my using of or statement the code above works for me:
public function scopeSearch($query, $val){
return $query->whereHas('user', function($query) use ($val) {
$query->where('tasks','like','%'.$val.'%')
->orWhere('name','like','%'.$val.'%');
})
->orWhereHas('customer', function($query) use ($val) {
$query->where('name', 'LIKE', '%'.$val.'%');
})
->with('user', 'customer');
}
Upvotes: 0
Reputation: 8082
You can follow the documentation about adding stuff to your relation query (whereHas
).
So, you should have this:
public function scopeSearch($query, $val){
return $query->with(['user', 'customer'])
->whereHas('user', function($query) use ($val) {
$query->where('tasks','like','%'.$val.'%')
->Orwhere('name','like','%'.$val.'%');
})
->whereHas('customer', function($query) use ($val) {
$query->where('name','like','%'.$val.'%');
});
}
See that you had only used whereHas
for users
but not customers
...
Upvotes: 1