Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call to undefined method Illuminate\Database\Query\Builder::save()

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

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();

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?

like image 886
Malchesador Avatar asked Jun 03 '14 19:06

Malchesador


2 Answers

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

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

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

See http://laravel.io/forum/06-04-2014-symfony-component-debug-exception-fatalerrorexception-call-to-undefined-method-illuminatedatabaseeloquentcollectionsave for additional reference.

like image 163
Malchesador Avatar answered Nov 02 '22 07:11

Malchesador


you have to fetch your model after given an ->where

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

or

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

if you don't do that your Object $this->employee would't be one and you could not use ->save()

like image 10
EselDompteur Avatar answered Nov 02 '22 08:11

EselDompteur