Trigger model events on nested mutation delete of belongsTo relationships#2588
Trigger model events on nested mutation delete of belongsTo relationships#2588skaesdorf wants to merge 6 commits into
Conversation
|
This would probably also apply to |
|
True, but thats just the case for |
|
Would it be simpler to let Laravel handle the fetching by using |
|
I think thats not possible for |
|
I just stumbled across an edgecase pitfall of
public function verifiedUsers () : BelongsTo
{
return $this->belongsTo( User::class )->onlyVerified();
}... it will not be respected since it is a) lost when calling |
|
@spawnia Any thoughts? I would like to solve this. |
|
Hi, can we please merge this? |
There was a problem hiding this comment.
Pull request overview
This PR changes nested delete behavior for belongsTo/hasOne-like relationships so the related model is first loaded and then deleted via Eloquent model instance deletion, ensuring model events (e.g. deleting) are fired during nested mutations.
Changes:
- Update nested delete implementation to call
first()?->delete()instead of relation-leveldelete(), enabling Eloquent model events. - Add integration tests asserting
deletingevents fire for nested deletes in both directive-based and nested-input mutation paths. - Minor test setup adjustments to create/associate related models consistently.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| tests/Integration/Schema/Directives/DeleteDirectiveTest.php | Adds tests verifying nested @delete(relation: ...) triggers model events; adjusts relation setup. |
| tests/Integration/Execution/MutationExecutor/BelongsToTest.php | Adds tests verifying nested belongsTo delete in input mutations triggers model events; tweaks setup assertions. |
| src/Schema/Directives/DeleteDirective.php | Switches hasOne/belongsTo-like nested delete from relation delete to model-instance delete. |
| src/Execution/Arguments/NestedBelongsTo.php | Switches nested belongsTo delete from relation delete to model-instance delete. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| $relation->dissociate(); | ||
| $relation->getParent()->save(); | ||
| } | ||
|
|
||
| $relation->delete(); | ||
| $relation->first()?->delete(); |
| $relation->dissociate(); | ||
| $relation->delete(); | ||
| $relation->first()?->delete(); |
I let Copilot review - I think the comments at least show some gaps in the test cases. I would like to have the behavior nailed down. |
Changes
When deleting a model using a nested belongs to relation, the model will now be queried and then deleted. This ensures model events to be triggered.
Breaking changes
None.