DEV Community

Cover image for The Ease of Eager-loading in Laravel
Drew Clements
Drew Clements

Posted on

The Ease of Eager-loading in Laravel

Ever waited for a webpage or app to load? That frustrating feeling when time seems to crawl by? That's the nightmare of today's digital world and a major buzzkill for users. Slow load times can derail the entire user experience, leading them to leave your website before even seeing its value.

That's a problem.

47% of users won't wait more than two seconds for a website to load
- Top Website Statistics of 2023 | Forbes

Thankfully, Laravel has a not-so-secret weapon for optimizing performance: Eager-loading.

What is Eager-loading?

Eager-loading is like saying, "I'll need that later, so I'll grab it now." It's akin to packing a flashlight for a trip instead of going back for it when needed.

In database terms, eager-loading reduces the number of trips to fetch data, saving milliseconds that add up with complex applications.

Eager-loading documentation

Eager-loading Prerequisites

In Laravel, eager-loading is built-in; no additional libraries are needed. However, proper setup of model relationships in the database and Laravel app is crucial.

Setting Up Your Tables

Define primary and foreign keys in your database tables. For example:

// posts table
$table->primary('id');

// comments table
$table->primary('id');
$table->foreignId('post_id')->constrained('posts');
Enter fullscreen mode Exit fullscreen mode

Defining Your Relationships

Define relationships in models generated by php artisan make:model ModelName. For instance:

// App\Models\Post.php
public function comments(): HasMany
{
  return $this->hasMany(PostComments::class);
}

// App\Models\PostComment.php
public function post(): BelongsTo
{
  return $this->belongsTo(Post::class);
}
Enter fullscreen mode Exit fullscreen mode

Eager-loading in Action

Now, how do we eager-load these relationships? Use $posts->load(['comments']) to fetch posts and associated comments in one query.

Without eager-loading, separately fetching posts and comments would lead to multiple queries, impacting performance significantly.
Now, let's look at how you could load posts and their comments without eager-loading.

In these examples, we'll act as though we're in a IndexPostsController where we'll query for all posts and their comments.

Without Eager-loading

// without eager loading

public function __invoke()
{
  $posts = Post::all();

  foreach ($posts as $post) {
    $posts->comments = PostComment::where('post_id', $post->id);
  }

  return Inertia::render('IndexPosts',
    [
      'posts' => $posts
    ]
  );
}
Enter fullscreen mode Exit fullscreen mode

With Eager-loading

// with eager loading

public function __invoke()
{
  return Inertia::render('IndexPosts',
    [
      'posts' => $posts->load(['comments'])
    ]
  );
}
Enter fullscreen mode Exit fullscreen mode

Eager-loading saves queries, improves performance, and enhances the user experience. Your database, you, and your users will thank you for building a fast-loading app!

Learn more about eager-loading here in the official Laravel documentation.

Top comments (1)

Collapse
 
sourovpal profile image
Info Comment hidden by post author - thread only accessible via permalink
Sourov Pal

Hi,
This is Sourov Pal. I am a freelance web developer and Software Developer. I can do one of project for free. If you like my work you will pay me otherwise you don't need to pay. No upfront needed, no contract needed. If you want to outsource your work to me you may knock me.

My what's app no is: +8801919852044
Github Profile: github.com/sourovpal
Thanks

Some comments have been hidden by the post's author - find out more