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

39,088

Solution 1

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.

Solution 2

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

Solution 3

Maybe more details for better understanding

With your Employee model :

if you make

Employee::find(1)

This will return a Eloquent model object (Instance of Employee Class)

if you make

Employee::where('field',op,value)

This will return a Builder object (Instance of Laravel Builder class)

if you make

Employee::where('field',op,value)->get()

This will return a Collection of your object (List of Employee instance)

if you make

Employee::where('field',op,value)->first()

This first() execute the get() function and then return the first element of the collection.

Laravel models extends Model class, and in this Model class is the save() function. so to be able to call it you have to have an instance of your model and not something else.

RM : be aware of the first() function, if there is 2 elements returned this result behave like there is only one result and its not true. in your case the login is declared as unique in the db so it should not be a problem, but be aware for another situations.

Share:
39,088

Related videos on Youtube

Malchesador
Author by

Malchesador

Web applications and back end developer since 2002.

Updated on July 09, 2022

Comments

  • Malchesador
    Malchesador almost 2 years

    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?

  • Malchesador
    Malchesador almost 10 years
    Interesting. That worked. It wasn't clear in the docs, nor in that other stackoverflow post I linked, and if I were to follow the convention in the Laravel docs (which uses $obj->find(1)) then it would seem that the ->get() method isn't needed, but it is. Thanks for the reply.
  • Malchesador
    Malchesador almost 10 years
    Well I could have swore that was working last night before I went home, but running it again this morning and now I'm getting Symfony \ Component \ Debug \ Exception \ FatalErrorException Call to undefined method Illuminate\Database\Eloquent\Collection::save() At least we have finally gotten into the Eloquent namespace. :P
  • phoops
    phoops almost 10 years
    You might have had a cached config or something. Using first() is the way to go.
  • EselDompteur
    EselDompteur almost 10 years
    @Malchesador: Your are right. ->find(1) doesn't need a following ->get or ->first() because it use direct the primary key of the table as parameter. The result will in this case an object with exact these row of content. In the case of ->where the resultset is not clear and so you have to fetch it ...
  • itsazzad
    itsazzad about 9 years
    Is it a relevant answer?