Q:

Search in Laravel after eager loading the records?

I have a flights table which has two foreign key columns: origin and destination, both of them are linked to airports table. Now I can eager load the records fine:
Flight::with('origin', 'destination')->get();

But now I want to do something like: 
Flight::with('origin', 'destination')->where('origin.name', 'LIKE', "$query%")->get();

But it is giving me an error saying that there is no column by the name of origin.name which is making sense to me but how do I search records within the eager loaded data?

A:

whereHas should work:
Flight::with('origin', 'destination')->whereHas('origin', function($q) use ($query){
    $q->where('name', 'LIKE', "$query%");
})->get();

