【问题标题】:Call to undefined method Illuminate\Database\Query\Builder::save()?调用未定义的方法 Illuminate\Database\Query\Builder::save()?
【发布时间】:2014-08-18 22:44:26
【问题描述】:

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

调用未定义的方法 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 版本是稳定版。

【问题讨论】:

    标签: laravel-4 eloquent


    【解决方案1】:

    问题是你没有得到你的用户的第一个实例,你只是在查询本身上调用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();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-04-23
      • 2019-09-29
      • 2020-01-04
      • 2018-10-03
      • 1970-01-01
      • 2019-04-02
      • 2016-11-25
      相关资源
      最近更新 更多