是否调用未定义的方法IlllightateDatabaseQueryBuilder::save()? [英] Call to undefined method IlluminateDatabaseQueryBuilder::save()?

查看:25
本文介绍了是否调用未定义的方法IlllightateDatabaseQueryBuilder::save()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

调用未定义的方法IlllightateDatabaseQueryBuilder::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();

这篇关于是否调用未定义的方法IlllightateDatabaseQueryBuilder::save()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆