[英]Laravel 4.2 Soft Delete not working
我使用laravel 4.2.8和Eloquent ORM。 当我尝试softdelete它不工作。 它从我的数据库中删除数据。 我想在逻辑上不删除数据。 在这里,我给出了我尝试的代码
模型
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;
了。
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;
});
}
}
你需要像这样使用特征;
use SoftDeletingTrait;
在你班级的开头。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.