我有要约和服务表。

服务是要约的子项。到目前为止,我已经建立了软删除商品的功能。我还将如何软删除附加服务?这是我的代码:

迁移优惠

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


移民服务

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');
});


模型报价

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

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


模型服务

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


删除方式

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


感谢您的所有帮助。

最佳答案

我已将此代码放在Offer模型中:

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

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


并增加了缺失

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


在服务模型中。

10-07 19:25
查看更多