Remove Global Eager Loading with withoutRelation()

Punyapal Shah
Use withoutRelation() or unsetRelation() to remove eager loaded relationships on specific Eloquent queries or model instances.

Models with $with properties eager load relations globally on every query. Use withoutRelation() on query builders to bypass global eager loads when relations are unneeded.

use App\Models\Post;

// Model defines protected $with = ['author', 'comments'];

// Query: Exclude 'comments' relation for this specific query
$posts = Post::withoutRelation('comments')->get();

// Instance: Unset loaded relation on an existing model object
$post->unsetRelation('comments');
  • Bypasses default $with eager loading definitions per-query
  • unsetRelation() clears in-memory relation data on model instances
  • Reduces unnecessary database subqueries for specific lightweight queries
Tags: #Laravel #Eloquent #Relationships
Share on X

// Found an issue or want to contribute a tip? github.com/MrPunyapal/tips