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'));
});
}
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With