Laravel - Dev版本会调用未定义的方法stdClass

时间:2014-03-01 19:21:19

标签: laravel

我有以下功能,通过电子邮件发送的网址注册后激活用户。

public function getActivate($code) {
    $user = User::where('code', '=', $code)->where('active', '=', 0);

    if($user->count()) {
        $user = $user->first();

        //Change user's active state 
        $user->active = 1;
        $user->code = '';

        if($user->save()) {
            return Redirect::route('home')
                    ->with('global', 'Activated! You can now sign in!');
        }
}
    return Redirect::route('home')
            ->with('global', 'We could not activate your account. Try again later.');
}

这是有效的,但现在我得到的就是这个错误:

Symfony \ Component \ Debug \ Exception \ FatalErrorException
Call to undefined method stdClass::save()

4 个答案:

答案 0 :(得分:1)

这是最新dev版本中的错误。根据{{​​3}}。任何稳定版本都不存在该错误。将您的composer.json minimum-stability更改为stable并运行作曲家更新。有关Githubthis answer的更多信息。

答案 1 :(得分:0)

尝试在查询结尾附加 get 方法,以便返回集合。当你有集合(这是Laravel对象集合)时,你将能够在那些对象上调用方法。

答案 2 :(得分:0)

我的症状非常相似,但就我而言,问题出在表格架构定义中:

        $table->integer('my_id')->unsigned();

需要:

        $table->increments('my_id')->unsigned();

答案 3 :(得分:0)

我刚才有同样的问题 但我不能稳定@The Alpha建议,因为它运行React.js,它需要一个开发版

所以这就是我如何解决它

而不是

>>> $note->body = 'Some note for the card.';
=> "Some note for the card."
>>> $note->card_id = 2;
=> 2
>>> $note->save();
[Symfony\Component\Debug\Exception\FatalThrowableError]  
  Call to undefined method stdClass::save() 

这就是我做的事情

>>> $note = new App\Note;
=> App\Note {#640}
>>> $note->body = 'Some note for the card.';
=> "Some note for the card."
>>> $note->card_id = 2;
=> 2
>>> $note->save();
=> true

它有效!干杯