Laravel : Call to undefined method Illuminate\Database\Eloquent\Collection::save()

39,564

The following line, uses the get method of Query Builder:

$tempFolder =   Message_Folders::where('userId',"=",1)->where('folderName',"=",$imap_folder['name'])->get();

It will retrieve all rows from that model (filtered by where condition of course). So you are getting a Collection instead of an instance of the model.

To retrieve a single row from your query you can use first method:

//returns a single row from the query
$tempFolder =   Message_Folders::where('userId',"=",1)->where('folderName',"=",$imap_folder['name'])->first();
Share:
39,564
Chintan Parekh
Author by

Chintan Parekh

Updated on July 09, 2022

Comments

  • Chintan Parekh
    Chintan Parekh almost 2 years

    I am basically trying to get a specific row from my table and update it, however, I end up getting a collection for which save method is not defined as the error says in my Title. Can you please tell me what is wrong in the below code. I have also tried it without the get method. But in that case I get a different error. Also, please let me know if there is any better approach to retrieve a row from a table with two primary keys(userId, folderName) in this case. Thank you :)

    $tempFolder =   Message_Folders::where('userId',"=",1)->where('folderName',"=",$imap_folder['name'])->get();
    if($tempFolder==null){//If not existing, however in this example assume that it exists
            $tempFolder =  new Message_Folders();
            $tempFolder->userId = 1;    
            $tempFolder->folderName = $imap_folder['name'];
            }
            $tempFolder->flags = $imap_folder['status']->flags;
            $tempFolder->messages = $imap_folder['status']->messages;
            $tempFolder->recent = $imap_folder['status']->recent;
            $tempFolder->unseen = $imap_folder['status']->unseen;
            $tempFolder->uidnext = $imap_folder['status']->uidnext;
            $tempFolder->uidvalidity = $imap_folder['status']->uidvalidity;
            $tempFolder->save();
    

    Edit 1:

    After Updating the code as mentioned in the first answer, now I get a new error.

    SQLSTATE[42S22]: Column not found: 1054 Unknown column 'id' in 'where clause' (SQL: update message_folders set recent = 4, updated_at = 2014-03-08 13:34:10 where id is null)

    Please refer to the code below:( Also, if all the values are same then it works fine(since the update is actually not happening))

    $tempFolder =   $this->folders->where('userId',"=",1)->where('folderName',"=",$imap_folder['name'])->first();
            if($tempFolder==null){
            $tempFolder =  new Message_Folders();
            $tempFolder->userId = 1;    
            $tempFolder->folderName = $imap_folder['name'];
            }
            $tempFolder->flags = $imap_folder['status']->flags;
            $tempFolder->messages = $imap_folder['status']->messages;
            $tempFolder->recent = 4;
            $tempFolder->unseen = $imap_folder['status']->unseen;
            $tempFolder->uidnext = $imap_folder['status']->uidnext;
            $tempFolder->uidvalidity = $imap_folder['status']->uidvalidity;
    
            $tempFolder->save();
    

    Edit 2: Part of migration file for the table mentioned in the above code:

    public function up()
        {
            Schema::create('message_folders', function(Blueprint $table) {
               $table->integer('userId');
               $table->string('folderName', 200);
               $table->integer('flags');
               $table->integer('messages');
               $table->integer('recent');
               $table->integer('unseen');
               $table->integer('uidnext');
               $table->integer('uidvalidity');
               $table->timestamps();
               $table->primary(array('userId','folderName'));
            });
        }
    
  • Chintan Parekh
    Chintan Parekh about 10 years
    Thanks for the quick reply. So, if I understand it right then even if the where clause return 1 element it would be returned as collection with 1 element?Right?
  • Chintan Parekh
    Chintan Parekh about 10 years
    So, basically, I just tried it with your suggestion and now I am getting a new error. Please check the updated code. I think it is trying to find a column named id (and I don't know why since I have no column named id)
  • marcanuy
    marcanuy about 10 years
    Yes, if you only want a single row then use first. If you are accessing a dynamic property, then Laravel knows if it has to call the get method (for one-to-many relationships) or first method (for one-to-one relationships).
  • marcanuy
    marcanuy about 10 years
    Check for you model or database migration, there should be an id column somewhere.
  • Chintan Parekh
    Chintan Parekh about 10 years
    Well, thanks, for the above answer. I already did check it. There is no Id column. Do you want me to share the code for my migration too?
  • Chintan Parekh
    Chintan Parekh about 10 years
    Also, to add to it. It works very fine if I put in the same old values. It throws no error.
  • marcanuy
    marcanuy about 10 years
    Did you declare your PK columns in the migrations file?
  • Chintan Parekh
    Chintan Parekh about 10 years
    So, well I have updated my question with the migration file. Could it be anything to do with the Model? I don't have any primary key defined there. Am I suppose to define a primary key there too? If yes, then how am I suppose to mention 2 primary keys? As an array?
  • Chintan Parekh
    Chintan Parekh about 10 years
    So, well, it turns out that Laravel by default expects a primary key named 'id' and hence throws this error. If I change $primaryKey to one of my primary key then it works. However, if I initialize it as an array then it fails. So, I believe there is no mechanism to add multiple primary key inside a model. Or, is there any? Also, can you please also add this to your current answer, would be helpful for someone. Anyways I am marking it as the right answer :). Please correct me if there is an option to add multiple primary keys in model. Thank you so much for your quick answers :)..
  • marcanuy
    marcanuy about 10 years
    Just find out that Eloquent only expects a single primary key. You can override the update method like explained here: forumsarchive.laravel.io/viewtopic.php?pid=41384#p41384
  • Chintan Parekh
    Chintan Parekh about 10 years
    Interesting, I will keep a note of this and update it in my code. Thanks a ton man.! :)