Reputation:
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');
}
Upvotes: 4
Views: 3231
Reputation: 11057
You need to use the trait like so;
use SoftDeletingTrait;
at the beginning of your class.
Upvotes: 0
Reputation: 1769
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;
});
}
}
Upvotes: 8