Skip to content

[12.x] Add Model::withoutRelation() for selective relation unloading #59137

Merged
taylorotwell merged 2 commits into
laravel:12.xfrom
SanderMuller:feature/model-without-relation
Mar 9, 2026
Merged

[12.x] Add Model::withoutRelation() for selective relation unloading #59137
taylorotwell merged 2 commits into
laravel:12.xfrom
SanderMuller:feature/model-without-relation

Conversation

@SanderMuller

Copy link
Copy Markdown
Contributor

Adds withoutRelation(string|array $relations) which clones the model and unsets only the specified relations, leaving the rest intact.

Motivation

Laravel provides withoutRelations() to clone a model without any loaded relations, and unsetRelation() to
mutate a model in place. But there's no non-mutating way to selectively remove specific relations:

// Remove all relations (clone) — exists
$clean = $model->withoutRelations();

// Remove one relation (mutates) — exists
$model->unsetRelation('comments');

// Remove specific relations without mutating — missing
$lightweight = $model->withoutRelation('comments');

This comes up when passing models to contexts where certain relations cause issues: circular references during serialization, unnecessary memory overhead in queued jobs, or Livewire/Inertia payloads where you want to strip heavy relations before sending to the frontend:

$block->setRelation('overview', $this->withoutRelation('blocks'));

Test plan

  • Single relation: clones model, removes only the specified relation, original unchanged
  • Array of relations: removes multiple relations selectively, keeps the rest
  • Existing withoutRelations() behavior unchanged

@shaedrich

Copy link
Copy Markdown
Contributor

While we use withers all across the framework, I wonder if it could be confused with with() and without() 🤔

@taylorotwell taylorotwell merged commit a06041f into laravel:12.x Mar 9, 2026
70 checks passed
jonagoldman pushed a commit to deplox/laravel-framework that referenced this pull request Apr 30, 2026
…aravel#59137)

* Add Model::withoutRelation() for selective relation unloading

* Update parameter type for withoutRelation method

---------

Co-authored-by: Taylor Otwell <taylor@laravel.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants