Multiple Level eager loading in Laravel

Updated at 13-Jan-2022, By samar

Multiple Level eager loading in Laravel

In this session, we’ll try our hand at solving the "Multiple Level eager loading in Laravel" puzzle by using the computer language.

You can load data by eager loading to multiple levels using one statement In Laravel, in this code snippet we not only load the author relationship data but also the country relation on the author model.
  • Multi Level eager loading in Laravel

    $data = App\Models\Book::with('author.country')->get();
    

    This code snippet will help you to get the books data along with the information of the book author with their country details. You have to define the author method with (belongsTo) relationship in the book model and the country method (hasOne relationship) in the author model.

Back to code snippet queries related laravel