Use loadMissing() to eager-load relationships only if they have not already been loaded on the model instance.
When passing models through service layers, notification handlers, or Blade components, calling $post->load('author') forces a new database query even if author was already eager-loaded upstream.
The loadMissing() method checks if the relationship is already loaded before executing any query.
Basic Usage
use App\Models\Post;
public function formatPost(Post $post): array
{
// Executes query ONLY IF 'author' and 'comments' are not already loaded
$post->loadMissing(['author', 'comments.user']);
return [
'title' => $post->title,
'author' => $post->author->name,
'count' => $post->comments->count(),
];
}
Difference Between load() and loadMissing()
$model->load('relation'): Always executes a new database query to reload the relation.$model->loadMissing('relation'): Checks$model->relationLoaded('relation')first, querying only if missing.
Summary
- Prevents duplicate database queries across multi-layered services.
- Supports nested relationships using dot notation (
comments.user). - Ideal for helper functions and formatting pipelines.
Related Tips
View all tips →Efficient Unique Slug Generation in Eloquent Without Looping Queries
Generate unique database slugs in Eloquent by querying the maximum existing numerical suffix directly instead of executing repeated queries in a while loop.
Scope Pivot Table Queries with Closures in wherePivot() in Laravel 13.26
Laravel 13.26 allows wherePivot() and orWherePivot() to accept closures, enabling direct reuse of local scopes defined on custom Pivot models.