调用未定义的方法Illuminate \ Database \ Query \ Builder :: save() [英] Call to undefined method Illuminate\Database\Query\Builder::save()

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

问题描述

我试图在现有记录上调用Eloquent的save()方法,但从Illuminate的查询生成器中收到错误.

I am trying to call Eloquent's save() method on an existing record but getting an error from Illuminate's Query Builder.

遵循Laravel网站上的文档,网址为 http://laravel.com/docs /eloquent#insert-update-delete 用于更新检索到的模型,并在此处查看示例:

Following the documentation on Laravel's web site at http://laravel.com/docs/eloquent#insert-update-delete for updating a retrieved model, and also looking at the example here: Laravel Eloquent ORM save: update vs create, my code appears to follow the expected convention, but instead I get the error mentioned in the title of this post.

$this->employee                  = Employee::where('login', $login);
$this->employee->first_name         = 'John';
$this->employee->last_name          = 'Doe';
$this->employee->save();

Employee类扩展了Eloquent,如果我创建模型的新实例,然后设置一些属性,然后调用save()方法,它将执行插入语句.我想念什么?

The Employee class extends Eloquent, and if I create a new instance of the model and then set some attributes and then call the save() method it performs the insert statements just fine. What am I missing?

推荐答案

显然,->get()方法不适用于Eloquent的->save()方法,而必须使用->first().

Apparently the ->get() method will not work with Eloquent's ->save() method and you must use ->first() instead.

更正: $this->employee = Employee::where('login', $login)->first();

Correct: $this->employee = Employee::where('login', $login)->first();

错误: $this->employee = Employee::where('login', $login)->get();

Incorrect: $this->employee = Employee::where('login', $login)->get();

请参见 http://laravel.io/forum/06-04-2014-symfony-component-debug-exception-fatalerrorexception-call to undefinedmethod-illuminatedatabaseeloquentcollections保存供其他参考.

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

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