Laravel Eloquent - 软删除不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23769908/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 09:32:50  来源:igfitidea点击:

Laravel Eloquent - Soft deleting is not working

laraveleloquent

提问by Kousha

I have added $table->softDeletes()to my migration file and have added the protected $softDelete = true;to my model, and I do have the deleted_atin my table. But whenever I call delete(), the entire row is deleted. Here is how I am calling my delete:

我已经添加$table->softDeletes()到我的迁移文件中,并且已经添加protected $softDelete = true;到我的模型中,并且deleted_at我的表中确实有。但是每当我打电话时delete(),整行都会被删除。这是我如何调用我的删除:

Schedule::whereId($id)->whereClientId($client_id)->delete();

What am I doing wrong?

我究竟做错了什么?

回答by Ikong

In case somebody bumps into this question. You can still use SoftDeletes in Laravel 5. Just use the softdeletes trait.

万一有人碰到这个问题。你仍然可以在 Laravel 5 中使用 SoftDeletes。只需使用 softdeletes 特性。

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 = [

    ];
}

You can view laravel docsor This great tutorial on how to softdelete

您可以查看laravel 文档有关如何进行软删除的精彩教程