How to force eager load in Laravel

Categorized as Laravel Tagged ,

You can force your app to eager load in Laravel using this method in the Service Provider.

This code will throw an exception if you don’t eager load a relationship (except in production).

// app/Providers/AppServiceProvider.php

use Illuminate\Database\Eloquent\Model;

public function boot()
{
  Model::preventLazyLoading(
    !$this->app->isProduction()
  );
}

Leave a reply

Your email address will not be published.