Search code examples
laravellaravel-collection

Will laravel reconnect to the database for any where on returned model collection?


If we have a find like this:

$returnedModel = Flight::
with("passengers")
->find(1);

Now if I add a where on returnedModel like this:

$returnedModel->passengers()->where("name" , "hamid")->first();

Will Laravel reconnect to the database for any where on returned model collection?

I added DB::getQueryLog() and there is a query with this where! how can I get this without reconnect to database?


Solution

  • You need to access your passengers as a property, not a method, by the way you get a Laravel collection already loaded from the DB (thanks to the with eager-loading).

    $returnedModel->passengers->firstWhere('name', 'hamid');