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

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

问题描述

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

I'm getting this error after trying to save my model. This is the error I'm getting:

调用未定义的方法 IlluminateDatabaseQueryBuilder::save()

Call to undefined method IlluminateDatabaseQueryBuilder::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 版本是稳定版.

My version of Laravel is the stable one.

推荐答案

问题是您没有获得用户的第一个实例,而您只是在查询中调用了 save()本身.

The problem is you are not getting the first instance of your user, and you are just calling the save() on the query itself.

这是更新的代码:

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) 替换为

Also, you may simplify your query build by replacing where($column, '=', $query) to

$user = User::whereCode($code)->whereActive(0)->first();

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

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