php – Laravel 4.2软删除不起作用
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了php – Laravel 4.2软删除不起作用,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1534字,纯文字阅读大概需要3分钟。
内容图文
![php – Laravel 4.2软删除不起作用](/upload/InfoBanner/zyjiaocheng/818/a18e1b4c0e6f4054bec9c502a072c9cc.jpg)
我使用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开始,你需要使用SoftDeletingTrait;现在,没有设置受保护的$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;
});
}
}
内容总结
以上是互联网集市为您收集整理的php – Laravel 4.2软删除不起作用全部内容,希望文章能够帮你解决php – Laravel 4.2软删除不起作用所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。