why do you need to keep the pivot row if the relation is detached?
Aug 6, 2025
3
Level 1
What to use for controllers with pivot tables soft deletes?
Hello I am a beginner in laravel, I want to ask for wisdom on how to approach adding and deleting multiple table columns with soft deletes considering that this model has a pivot table
This is my procedures relationship in my Plan model
public function procedures(): BelongsToMany
{
return $this->belongsToMany(Procedure::class, 'plan_procedures')
->using(PlanProcedure::class)
->withPivot(['notes', 'deleted_at'])
->wherePivotNull('deleted_at')
->withTimestamps();
}
Here is how i attach relationships in my createPlan service
if (!empty($data['procedure_id'])) {
$plan->procedures()->syncWithoutDetaching([
$data['procedure_id'] => [
'notes' => $data['procedure_notes'],
]
]);
}
Here is how i detach my relationship in my deletePlan service
DB::table('plan_procedures')->where('plan_id', $plan->id)->whereNull('deleted_at')->update(['deleted_at' => $now]);
Please or to participate in this conversation.