FarhadMohammadi's avatar

belogsToMany relation withoutTrashed()?

How can I fetch belongsToMany records without trashed rows? for example like:

	$consultation->insurances()->withoutTrashed();

is it possible?

0 likes
6 replies
Nakov's avatar

Do you have the use SoftDeletes trait in your model? If you do, then that's the default behaviour. You won't need to make without trashed call.

newbie360's avatar

when you use SoftDelete, all the query will append and deleted_at is null

suggest install Laravel-debugbar

1 like
FarhadMohammadi's avatar

@newbie360 yes we have a ConsultationInsurace pivot model and uses SoftDeletes but I have to append wherePivotNull('deleted_at') to relations like

 $this->belongsToMany(Insurance::class)->wherePivotNull('deleted_at')->withPivot('percentage');
FarhadMohammadi's avatar

@newbie360 ya :) me neither have no Idea :) cuz some foreign key cascade we can't hard delete the records and we have to soft delete them, so sync will not work with soft delete and I have to implement it manually.

newbie360's avatar
Level 24

@FarhadMohammadi why ? if in the pivot table both foreign key is deletable, why not just in migration set both foreign key cascade on delete

1 like

Please or to participate in this conversation.