Skip to main content

Eloquent Performance Tricks

Only get what you need

Eloquent by default will do select everything from your table, in most cases this is a bad idea. Limit this by using the select() method.

$apples = Apple::select('name', 'region', 'deliciousness')->all();

Delay the eager loading of a relationship

There are times where the eager loaded the relationship may not be needed until a specific condition is met. In this case it may be worth delaying the eager load until it will be required by using the load() function.

$bannana = Bannana::all();

if($bannana->inSeason()) {
// Delayed eager load
$bannana->load('region');
}