gpt4 book ai didi

laravel-4 - 调用未定义的方法 Illuminate\Database\Query\Builder::save()?

转载 作者:行者123 更新时间:2023-12-03 08:10:44 24 4
gpt4 key购买 nike

我在尝试保存我的模型后遇到此错误。这是我得到的错误:

Call to undefined method 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) 替换为

来简化查询构建
$user = User::whereCode($code)->whereActive(0)->first();

关于laravel-4 - 调用未定义的方法 Illuminate\Database\Query\Builder::save()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25373185/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com