Laravel 4.2软删除不起作用

4
我使用laravel 4.2.8和Eloquent ORM。 当我尝试软删除时,它无法正常工作。它会从我的数据库中删除数据。 我想逻辑上删除数据而不是物理上删除。 这里是我尝试过的代码。
模型:
use Illuminate\Auth\UserInterface;
use Illuminate\Database\Eloquent\SoftDeletingTrait;

class User extends Eloquent implements UserInterface {

    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'users';
        public $timestamps = true;
        protected $softDelete = true;
        protected $dates = ['deleted_at'];

        public static function boot()
        {
            parent::boot();
            static::creating(function($post)
            {
                $post->created_by = Auth::user()->id;
                $post->updated_by = Auth::user()->id;
            });

            static::updating(function($post)
            {
                $post->updated_by = Auth::user()->id;
            });

            static::deleting(function($post)
            {
                $post->deleted_by = Auth::user()->id;
            });
        }
}

Controller

public function destroy($id) {
        // delete
        $user = User::find($id);
        $user->delete();

        // redirect
        return Redirect::to('admin/user');
    }
2个回答

8

从4.2版本开始,你需要使用SoftDeletingTrait,而不是再设置protected $softDelete = true;

use Illuminate\Auth\UserInterface;
use Illuminate\Database\Eloquent\SoftDeletingTrait;

class User extends Eloquent implements UserInterface {

    use SoftDeletingTrait;

    protected $table = 'users';
    public $timestamps = true;
    protected $dates = ['deleted_at'];

    public static function boot()
    {
        parent::boot();
        static::creating(function($post)
        {
            $post->created_by = Auth::user()->id;
            $post->updated_by = Auth::user()->id;
        });

        static::updating(function($post)
        {
            $post->updated_by = Auth::user()->id;
        });

        static::deleting(function($post)
        {
            $post->deleted_by = Auth::user()->id;
        });
    }
}

0

你需要这样使用 trait;

use SoftDeletingTrait;

在你的类的开头。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接