Laravel:调用未定义的方法 Illuminate\Database\Eloquent\Collection::save()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22269601/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 02:45:12  来源:igfitidea点击:

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

laravellaravel-4

提问by Chintan Parekh

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

我基本上是想从我的表中获取特定行并更新它,但是,我最终得到了一个集合,其中的保存方法未定义为我的标题中的错误。你能告诉我下面的代码有什么问题吗?我也试过没有 get 方法。但在那种情况下,我得到了一个不同的错误。另外,请让我知道在这种情况下是否有更好的方法从具有两个主键(userId、folderName)的表中检索行。谢谢 :)

$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:

编辑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_foldersset recent= 4, updated_at= 2014-03-08 13:34:10 where idis null)

SQLSTATE[42S22]:未找到列:1054 'where 子句'中的未知列 'id'(SQL:更新message_foldersrecent= 4,updated_at= 2014-03-08 13:34:10 where idis 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:

编辑 2:上面代码中提到的表的迁移文件的一部分:

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'));
        });
    }

回答by marcanuy

The following line, uses the getmethod of Query Builder:

下面这行,使用了Query Builder的get方法:

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

It will retrieve all rowsfrom that model (filtered by wherecondition of course). So you are getting a Collection instead of an instance of the model.

它将从该模型中检索所有行(当然由where条件过滤)。所以你得到的是一个集合而不是模型的实例。

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

要从查询中检索一行,您可以使用一种方法:

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