Last Updated: September 29, 2021
·
2.273K
· usirin

Laravel 4 - auto eager load

I just found out that there is a way to eager load some relationships with Laravel 4 for every fetch you make. Following is from Model.php

/**
 * The relations to eager load on every query.
 *
 * @var array
 */
protected $with = array();

You just need to set $with array in your model.

protected $with = array('foo', 'bar');

just add this to your model, and everything in that array, will be eager loaded.

1 Response
Add your response

Awesome tip thanks!

over 1 year ago ·