Laravel 5: cascade soft delete

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.


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.


You should use Eloquent events for this.

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

Offers::restored(function($offer) {
    $offer->services()->withTrashed()->restore();
});