[英]Eloquent restore() function on soft deletes failing to restore
I recently added soft deletes on my user model and the delete part of it works perfectly, however when I try to restore I get an error that says Call to a member function restore() on a non-object
. 我最近在我的用户模型上添加了软删除,并且它的删除部分可以完美地工作,但是当我尝试还原时,出现一个错误,提示
Call to a member function restore() on a non-object
。
My code for restoring a soft deleted user is as follows: 我的恢复软删除用户的代码如下:
public function putActivateUser()
{
$user = Emp::onlyTrashed()->where('id', '=', Input::get('actEmpId'))->first();
$user->restore();
return Redirect::route('user_data')
->with('message', 'Bruker '.$user->user_name.' aktivert.');
}
The form for the user activation: 用户激活的形式:
{{ Form::open(array('url' => 'bassengweb/ressurect_user', 'method' => 'PUT')) }}
{{ Form::select('actEmpId', $deactEmps) }}
{{ Form::submit('Aktiver Bruker') }}
{{ Form::close() }}
A dd on $user returns null for some reason, but I can't see why. $ user上的dd由于某种原因返回null,但我不明白为什么。
尝试这个
Emp::withTrashed()->where('id','=',Input::get('actEmpId'))->restore();
Apparently user with id from the form is not found with onlyTrashed
scope. 显然,在表单中没有找到ID为ID的用户,并且其
onlyTrashed
范围为onlyTrashed
。 You should check the query (run DB::getQueryLog() for example) and data in your db, but first change the method to firstOrFail
: 您应该检查查询(例如,运行DB :: getQueryLog())和数据库中的数据,但首先将方法更改为
firstOrFail
:
$user = Emp::onlyTrashed()->where('id', '=', Input::get('actEmpId'))->firstOrFail();
It will throw ModelNotFoundException
if nothing was found, so you can catch it and do whatever is needed, thus avoid calling method on null error. 如果未找到任何内容,它将抛出
ModelNotFoundException
,因此您可以捕获它并执行所需的任何操作,从而避免在null错误上调用方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.