我在我的迁移文件中添加了$table->softDeletes()
,并在模型中添加了protected $softDelete = true;
。同时,在表中我也有deleted_at
这一字段。但是每当我调用delete()
方法时,整行记录都会被删除。以下是我的删除代码:
Schedule::whereId($id)->whereClientId($client_id)->delete();
我做错了什么?
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes; //<--- use the softdelete traits
class YourModel extends Model
{
use SoftDeletes; //<--- use the softdelete traits
protected $dates = ['deleted_at']; //<--- new field to be added in your table
protected $fillable = [
];
}
您可以查看Laravel文档或这篇关于如何软删除的优秀教程
\Illuminate\Database\Eloquent\Model::boot
方法,请确保在该方法中调用 parent::boot
,以便可以启动 traits(例如 SoftDeletes
)。
protected $softDelete = true;
已经设置好了。如果您运行dd(new Schedule());
,您应该能够看到"softDelete":protected]=> bool(true)
。 - Matteus Hemströmprotected 'softDelete' => boolean true
但我发誓它被删除了!而且我正在使用 Laravel 4.2。 - Koushavar_dump(DB::getQueryLog());
并分享结果。 - Razor