How To Use Laravel Eager Loading

0
870
How To Use Laravel Eager Loading

Using laravel eloquent relationships always resulted in “lazy loaded” by default. In this tutorial, you will learn how to use Laravel Eager Loading. Let’s Think of every employee has one job. To understand eager loading, you need Laravel One To One Relationship to connect both models. Please setup laravel project with an above link with laravel eloquent one to one relationship.

# Installing Laravel Debugbar

After setting up project install laravel debugging package with the following command. Using this package you will visually understand how eager loading works. You can skip installing this package, but its good practice to have this package in development mode in your all laravel project.

composer require barryvdh/laravel-debugbar --dev

Register the service into the app.php’s providers array.

/* config > app.php */

'providers' => [
    Barryvdh\Debugbar\ServiceProvider::class,
]

Similarly, register the facade into aliases array.

/* config > app.php */

'aliases' => [
    'Debugbar' => Barryvdh\Debugbar\Facade::class,
]

That’s It! You successfully configured laravel debugbar package. You can disable it by setting APP_DEBUG=false in to .env file. You will check used queries in query tab in debugbar.

Laravel Debugbar

# Understanding Eager Loading

Create TestController and put following logic into it. We are passing all employees in welcome page.

php artisan make:controller TestController
/* app > Http > Controllers > TestController.php */
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Employee;

class TestController extends Controller
{
    public function index()
    {
        $employees = Employee::all();
        return view('welcome', compact('employees'));
    }
}

Replace route in web.php

/* routes > web.php */
<?php

Route::get('/', 'TestController@index');

Into welcome page put following code to loop through all employees

/* resources > views > welcome.blade.php */
@foreach($employees as $employee)
    <p>Name: {{ $employee->name }}</p>
@endforeach

As you can see in debugbar laravel using one query to get all employees. Thats fine.

Debugbar Single Query

Now, find all employees company name from job table with one to one relationship. To do so update welcome page as below.

/* resources > views > welcome.blade.php */
@foreach($employees as $employee)
    <p>Name: {{ $employee->name }} <br> Job: {{ $employee->job->name_of_company }}</p>
@endforeach

This is lazy loading. N + 1 queries executed for finding all employees and their jobs. Where N is a number of employees. We have 10 records that’s why it’s using 11 queries. Let’s think of 1000 employees, there will be major performance issues.

Lazy Loading

Now, comes the eager loading to play. Laravel eager loading using with method. Update TestController a little bit.

/* app > Http > Controllers > TestController.php */
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Employee;

class TestController extends Controller
{
    public function index()
    {
        $employees = Employee::with('job')->get();
        return view('welcome', compact('employees'));
    }
}

Check welcome page. As you can see in debugbar, with eager loading you can get same result by using only 2 queries.

Eager Loading

Learn more about Laravel Framework

Previous articleHow To Use Laravel Eloquent One To Many Relationship
Next articleHow To Use Laravel Validation
Welcome to the world of web development, where technology and creativity come together to bring ideas to life. My name is Keyur Gadher, and as a web development blogger, I am here to share my knowledge and experience with you. From front-end web development to back-end programming, I will provide you with valuable tips, tricks, and techniques to help you create beautiful and functional websites that are tailored to your specific needs. Whether you're a beginner or a seasoned pro, I am here to help you take your web development skills to the next level. Join me today and let's start coding!

LEAVE A REPLY

Please enter your comment!
Please enter your name here