Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Soft delete has no effect on relations #5

Open
ChrisHelmsC opened this issue Aug 20, 2019 · 1 comment
Open

Soft delete has no effect on relations #5

ChrisHelmsC opened this issue Aug 20, 2019 · 1 comment

Comments

@ChrisHelmsC
Copy link

Maybe I'm misunderstanding what this is for. Lets say an event has a many to many relationship with users. This relation is "attendingUsers".

If I get a user through that relation and then detach it, I see deleted_at is populated in the pivot table. But the issue is when you run that relation again, you still get that user.

$event->attendingUsers->count();
=> 20
>>> $event->attendingUsers->first()->detach();
=> 1
>>> $event->attendingUsers->count();
=> 20

Seem like it would be considerably more useful if the relations would prevent associates with soft deleted relations. Am I missing something?

@soulcodex
Copy link

You need add on your relationship the condition to not get the nulled entries:

return $this->belongsToMany(Model::class)->whereNull('relation.deleted_at')->using(PivotTable::class)

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

No branches or pull requests

2 participants