Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

sizzlingsisig's avatar

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]);
0 likes
3 replies
krisi_gjika's avatar

why do you need to keep the pivot row if the relation is detached?

sizzlingsisig's avatar

hello thank you for responding. Sorry let me rephrase my question. What i actually want to do is when deleting a plan, I want the pivot table entry to be soft deleted too. What other approaches can I take to achieve this

Please or to participate in this conversation.