调用未定义的方法Illuminate \ Database \ Query \ Builder :: save() [英] Call to undefined method 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();
这篇关于调用未定义的方法Illuminate \ Database \ Query \ Builder :: save()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!