Laravel 4.2软删除不起作用 [英] Laravel 4.2 Soft Delete not working

查看:218
本文介绍了Laravel 4.2软删除不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用laravel 4.2.8和雄辩的ORM. 当我尝试软删除它不起作用时.它从我的数据库中删除数据. 我想从逻辑上而不是物理上删除数据. 在这里,我将尝试的代码告诉我

I Use laravel 4.2.8 and Eloquent ORM. When I try to softdelete its not working. It delete data from my database. I want to delete data logically not physically. Here I give my code what I tried

模型

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

控制器

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

        // redirect
        return Redirect::to('admin/user');
    }

推荐答案

从4.2开始,您现在需要use SoftDeletingTrait;,不再设置protected $softDelete = true;.

As of 4.2, you need to use SoftDeletingTrait; now, not set the protected $softDelete = true; anymore.

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

这篇关于Laravel 4.2软删除不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆