35

I am having offers and services table.

Service(s) is a child of an offer. So far I have established functionality for soft deleting an offer. How would I also soft delete appended services? Here is my code:

Migration Offers

Schema::create('offers', function(Blueprint $table)
{
    $table->increments('id')->unsigned();
    ...    
    $table->timestamps();
    $table->softDeletes();
});

Migration Services

Schema::create('services', function(Blueprint $table) 
{
    $table->increments('id');
    $table->integer('offer_id')->unsigned();   
    ...
    $table->timestamps();
    $table->softDeletes();
});

Schema::table('services', function($table) 
{
    $table->foreign('offer_id')
          ->references('id')
          ->on('offers');
});

Model Offer

use SoftDeletes;
protected $dates = ['deleted_at'];

public function services() {
    return $this->hasMany('App\Service');
} 

Model Service

public function offer() {
    return $this->belongsTo('App\Offer');
} 

Delete method

public function destroy($id)
{
    $offer = Offer::find($id);
    $offer->delete();
}

Thank you for all the help.

be-codified
  • 5,704
  • 18
  • 41
  • 65

4 Answers4

51

I have put this code in Offer model:

protected static function boot() {
    parent::boot();

    static::deleting(function($offer) {
        $offer->services()->delete();
    });
}

And added missing

use SoftDeletes;
protected $dates = ['deleted_at'];

in the Service model.

be-codified
  • 5,704
  • 18
  • 41
  • 65
  • 3
    but this solution doesn't work if services himself has FK... Deleting is not triggered in the child model – Juliatzin Feb 05 '16 at 00:35
  • I've been banging my head against the wall about why this did not work for me.. Found out it was because it doesn't call the `deleting` and `deleted` methods when directly making a delete call to the query. Here are some more details and examples of how to make it work: https://github.com/laravel/framework/issues/2536#issuecomment-74557441 – Arno van Oordt Oct 07 '17 at 07:59
  • When you need to cascade thorugh a few levels (from grandparent to child) you have to load the models via foreach as here explained: https://github.com/laravel/framework/issues/2536#issuecomment-101330841 – DumbergerL Jan 08 '20 at 13:05
7

You should use Eloquent events for this.

Offers::deleted(function($offer) {
    $offer->services()->delete();
});

Offers::restored(function($offer) {
    $offer->services()->withTrashed()->restore();
});
ceejayoz
  • 176,543
  • 40
  • 303
  • 368
5

If you want to get cascading softDeletes in your Eloquent Models I highly recommend using this library iatstuti/laravel-cascade-soft-deletes

Composer

// get it with composer.
$ composer require iatstuti/laravel-cascade-soft-deletes="1.0.*"

Quick example

The one provided in the getting started sample.

<?php

namespace App;

use App\Comment;
use Iatstuti\Database\Support\CascadeSoftDeletes;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Post extends Model
{
    use SoftDeletes, CascadeSoftDeletes;

    protected $cascadeDeletes = ['comments'];

    protected $dates = ['deleted_at'];

    public function comments()
    {
        return $this->hasMany(Comment::class);
    }
}  
2

You can do like this.

self::deleting(function($offer) {
    $offer->services()->delete();
});

self::restoring(function ($offer) {
   $offer->services()->restore();
});

You should first delete/restore the children records (services) before deleting/restoring the parent (offer). Failing to do this, will trigger referential integrity MySql error.

  • 4
    It can't trigger an integrity error because the records are just soft deleted. However, you'd restore the parent before restoring the children. So use `self::restored`. – halloei Apr 11 '16 at 11:32