Skip to content

Stop Eager Loading on the Model

Quick Tips•
October 19, 2026

Setting protected $with on a model eager loads those relations on every query the model builds, including the ones that never read them. A controller that calls Post::findOrFail($id) just to update a title also runs one query for author and one for comments. Each is a separate WHERE ... IN (...) query against the related table, and the controller shows no sign of them because the list lives in the model.

Don't do this

PHP
<?php declare(strict_types=1); namespace App\Models; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\HasMany; final class Post extends Model { protected $with = ['author', 'comments']; public function author(): BelongsTo { return $this->belongsTo(User::class); } public function comments(): HasMany { return $this->hasMany(Comment::class); } }

Do this instead

PHP
<?php declare(strict_types=1); namespace App\Models; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\HasMany; final class Post extends Model { public function author(): BelongsTo { return $this->belongsTo(User::class); } public function comments(): HasMany { return $this->hasMany(Comment::class); } }
PHP
<?php declare(strict_types=1); namespace App\Http\Controllers; use App\Models\Post; use Illuminate\Http\JsonResponse; final readonly class PostIndexController { public function __invoke(): JsonResponse { $posts = Post::query() ->with(['author', 'comments']) ->latest() ->get(); return new JsonResponse($posts); } }

Each query now declares what it needs. The endpoint listing posts loads author and comments, and the endpoint that updates a title loads neither. You can read a query and know its cost without opening the model. With Model::preventLazyLoading() switched on in development, reading a relation you did not request on a query that returned several models throws instead of quietly running one query per row.