$inst = Institution::find($inst->id); $campus = InstitutionCampusId::where('institution_campus_id.institution_id', $institution->id); $inst->delete(); $campus->delete();
uj5u.com熱心網友回復:
在您的模型中添加此特征
use SoftDeletes;
在您的遷移中添加它,它將在您的表中的欄位中添加洗掉
$table->softDeletes();
uj5u.com熱心網友回復:
級聯洗掉是在資料庫級別處理的,因此當您在遷移中設定 onDelete('cascade') 時,這會轉換為您的資料庫洗掉由外鍵附加的任何記錄。
軟洗掉由應用程式處理,因此您需要在父模型上觸發一個事件并在子模型上偵聽它,或者在您的父模型中,在引導方法中系結 static::deleted() 方法,并洗掉那里的關系。
我不確定您是否可以執行以下操作:
public static function boot()
{
parent::boot();
static::deleted(function ($model) {
// Probably lazy load these relationships to avoid lots of queries?
$model->load([ 'relationshipOne', 'relationshipTwo', ]);
$model->relationshipOne()->delete();
$model->relationshipTwo()->delete();
});
}
或者,如果您必須遍歷相關專案:
public static function boot()
{
parent::boot();
static::deleted(function ($model) {
$model->relationshipOne->each(function ($item) {
$item->delete();
});
$model->relationshipTwo->each(function ($item) {
$item->delete();
});
});
}
uj5u.com熱心網友回復:
向您的模型添加特征,use SoftDeletes;并在您的遷移中添加$table->softDeletes()架構創建的末尾(這將自動創建deleted_at列)
對于查詢,您可以使用 eloquent 方法僅獲取已洗掉或未洗掉的條目,例如:
僅獲取已洗掉的條目:$query->onlyTrashed(),
僅獲取未洗掉的條目:$query->withoutTrashed(),
要獲取所有條目(已洗掉): $query->withTrashed()
在垃圾箱中發送條目:$model->delete(),
對于永久洗掉條目使用: $model->forceDelete()
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/370931.html
