Laravel 4.2 Soft Delete not working
我使用laravel 4.2.8和雄辩的ORM。
当我尝试软删除它不起作用时。它从我的数据库中删除数据。
我想从逻辑上而不是物理上删除数据。
在这里,我将尝试的代码提供给我的代码
型号
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | 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; }); } } |
控制器
1 2 3 4 5 6 7 8 | public function destroy($id) { // delete $user = User::find($id); $user->delete(); // redirect return Redirect::to('admin/user'); } |
从4.2版本开始,您现在需要
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | 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; }); } } |
您需要像这样使用特征;
1 | use SoftDeletingTrait; |
在课程开始时。