Laravel 4.2 Soft delete does not work

I am using laravel 4.2.8 and Eloquent ORM. When I try to disable it, it does not work. It deletes data from my database. I want to logically delete data physically. Here I give my code to 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');
    }
+4
source share
2 answers

Starting with 4.2, you need use SoftDeletingTrait;now, and not install protected $softDelete = true;more.

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;
        });
    }
}
+6
source

You need to use this feature as follows:

use SoftDeletingTrait;

at the beginning of your class.

0
source

All Articles