Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Eager loading with parameters - laravel

I have Banks table and separate table with services

$bank = Banks::find(1);
echo $bank->service(1); // print bank with that service (serviceId 1)

It is posible to eager load all banks with service_id =1 ..somewhat like

Bank::with('service(1)')->get();

Thank you in advance

like image 897
sumit Avatar asked Dec 25 '14 07:12

sumit


1 Answers

Sure! The with method accepts a closure to filter eager loading.

Bank::with(array('service' => function($query){
    $query->where('id', 1);
}))->get();
like image 53
lukasgeiter Avatar answered Oct 09 '22 03:10

lukasgeiter