我在尝试保存模型后收到此错误。这是我收到的错误:
调用未定义的方法 Illuminate\Database\Query\Builder::save()
这是我的代码:
public function getActivate ($code)
{
$user = User::where('code','=',$code)->where('active','=',0);
if ($user->count())
{
$user->first();
//Update user to active state
$user->active = 1;
$user->code ='';
if($user->save())
{
return Redirect::route('home')
->with('global', 'Account Activated ! You can sign in ');
}
}
return Redirect::route('home')
->with('global', 'We could not activate your account. Try again later');
}
我的 Laravel 版本是稳定版本。
问题是您没有获得用户的第一个实例,而您只是调用save()
关于查询本身。
这是更新后的代码:
public function getActivate ($code)
{
$user = User::where('code','=',$code)->where('active','=',0)->first();
if ($user)
{
//Update user to active state
$user->active = 1;
$user->code ='';
if($user->save())
{
return Redirect::route('home')
->with('global', 'Account Activated ! You can sign in ');
}
}
return Redirect::route('home')
->with('global', 'We could not activate your account. Try again later');
}
此外,您可以通过替换来简化查询构建where($column, '=', $query)
to
$user = User::whereCode($code)->whereActive(0)->first();
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)