Optimize Your Laravel Queries with Lazy Eager Loading

Optimize Your Laravel Queries with Lazy Eager Loading

In Laravel, efficient database querying is crucial for maintaining high-performance applications. One of the tools at your disposal is lazy eager loading, implemented through the loadMissing() method. This feature allows you to load relationships on the fly, only when they're needed, helping you strike a balance between performance and flexibility.

Understanding loadMissing()

The loadMissing() method allows you to eager load relationships on an existing model or collection if they haven't been loaded already. This is particularly useful when you're not sure if a relationship has been loaded and want to avoid N+1 query problems.

$users->loadMissing(['posts' => function ($query) {
    $query->where('published', true);
}, 'comments.author']);

Real-World Example: Blog Platform with Dynamic Content Loading

Let's consider a blog platform where we need to display users, their posts, and comments, but we want to optimize our queries based on different view scenarios.

First, let's define our models:

class User extends Model
{
    public function posts()
    {
        return $this->hasMany(Post::class);
    }

    public function comments()
    {
        return $this->hasMany(Comment::class);
    }
}

class Post extends Model
{
    public function comments()
    {
        return $this->hasMany(Comment::class);
    }

    public function author()
    {
        return $this->belongsTo(User::class, 'user_id');
    }
}

class Comment extends Model
{
    public function author()
    {
        return $this->belongsTo(User::class, 'user_id');
    }
}

Now, let's implement a controller that uses loadMissing() to optimize our queries:

class BlogController extends Controller
{
    public function index()
    {
        $users = User::all();

        return view('blog.index', compact('users'));
    }

    public function showUserPosts(User $user)
    {
        $user->loadMissing(['posts' => function ($query) {
            $query->where('published', true)->orderBy('created_at', 'desc');
        }]);

        return view('blog.user_posts', compact('user'));
    }

    public function showPost(Post $post)
    {
        $post->loadMissing(['comments.author', 'author']);

        return view('blog.post', compact('post'));
    }

    public function showUserActivity(User $user)
    {
        $user->loadMissing(['posts' => function ($query) {
            $query->latest()->take(5);
        }, 'comments' => function ($query) {
            $query->latest()->take(10);
        }]);

        return view('blog.user_activity', compact('user'));
    }
}

In our views, we can now safely access these relationships without worrying about N+1 queries:

<!-- blog/index.blade.php -->
@foreach ($users as $user)
    <h2>{{ $user->name }}</h2>
    <a href="{{ route('blog.user_posts', $user) }}">View Posts</a>
@endforeach

<!-- blog/user_posts.blade.php -->
<h1>{{ $user->name }}'s Posts</h1>
@foreach ($user->posts as $post)
    <h2>{{ $post->title }}</h2>
    <p>{{ $post->excerpt }}</p>
@endforeach

<!-- blog/post.blade.php -->
<h1>{{ $post->title }}</h1>
<p>By {{ $post->author->name }}</p>
<div>{{ $post->content }}</div>
<h2>Comments</h2>
@foreach ($post->comments as $comment)
    <p>{{ $comment->content }} - {{ $comment->author->name }}</p>
@endforeach

<!-- blog/user_activity.blade.php -->
<h1>{{ $user->name }}'s Recent Activity</h1>
<h2>Recent Posts</h2>
@foreach ($user->posts as $post)
    <p>{{ $post->title }}</p>
@endforeach
<h2>Recent Comments</h2>
@foreach ($user->comments as $comment)
    <p>On post "{{ $comment->post->title }}": {{ $comment->content }}</p>
@endforeach

By leveraging loadMissing(), you can create more efficient and scalable Laravel applications, ensuring that you're only loading the data you need, when you need it.

If this guide was helpful to you, subscribe to my daily newsletter and give me a follow on X/Twitter. It helps a lot!

Subscribe to Harris Raftopoulos

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
jamie@example.com
Subscribe