You would not need to use with here, using callbacks with with method filters only the relationship i.e User not the Model itself i.e Album. Instead,
whereHas is probably what you need. You can read about it in docs
Here is how you can get desired results:
Album::where('title', 'LIKE', '%' .$input. '%') //Give me this album if its title matches the input
// I need this album if any of its user's name matches the given input
->orWhereHas('user', function($q) use ($input) {
return $q->where('name', 'LIKE', '%' . $input . '%');
})
// I need this album if any of its tracks' title matches the given input
->orWhereHas('tracks', function($q) use ($input) {
return $q->where('title', 'LIKE', '%'. $input . '%');
})->get();
This should work for you. I have not tested this, let me know if it works or not.
Update
I am assuming you have proper relatioships setup, maybe like this:
- An album belongs to a user (many to one)
- An album has many tracks (one to many).
But even if its not the case with you, you have got the idea now.