Laravel 4.2 Soft Delete not working

大城市里の小女人 提交于 2020-01-14 12:36:37

问题


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

model

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

回答1:


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



回答2:


You need to use the trait like so;

use SoftDeletingTrait;

at the beginning of your class.



来源:https://stackoverflow.com/questions/25501066/laravel-4-2-soft-delete-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!