laravel Eloquent 中的重复密钥更新
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38308555/
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
ON DUPLICATE KEY UPDATE in Eloquent
提问by Schwesi
I just started laravel and all I want to do is get following queryworking in Eloquent:
我刚开始laravel和所有我想要做的就是下面的查询工作雄辩:
INSERT INTO geschichte (geschichte_id,
geschichte_text1,
geschichte_text2,
geschichte_text3)
VALUES (:geschichte_id,
:geschichte_text1,
:geschichte_text2,
:geschichte_text3)
ON DUPLICATE KEY
UPDATE geschichte_id = :geschichte_id,
geschichte_text1 = :geschichte_text1,
geschichte_text2 = :geschichte_text2,
geschichte_text3 = :geschichte_text3;
Controller function
控制器功能
public function alterGeschichte(Request $request)
{
$geschichte1 = new Geschichte;
$geschichte2 = new Geschichte;
$geschichte3 = new Geschichte;
$geschichte1 = Geschichte::updateOrCreate(
['id' => 1],
['geschichte_text1' => $request->geschichte_text1]
);
$geschichte2 = Geschichte::updateOrCreate(
['id' => 2],
['geschichte_text2' => $request->geschichte_text2]
);
$geschichte3 = Geschichte::updateOrCreate(
['id' => 3],
['geschichte_text3' => $request->geschichte_text3]
);
$geschichte1->save();
$geschichte2->save();
$geschichte3->save();
return redirect('/geschichte');
}
The problem in more detail
I cannot get the 'on duplicate key update' part to work.
There always is a new entry created for every time I update. I would like the id always to be the same for every entry and just overwrite the older entry with that id.
I would be very thankful for any kind of help. I am struggling with this from hours...
UPDATE
更详细的问题
我无法使“重复密钥更新”部分起作用。
每次更新时总会创建一个新条目。我希望每个条目的 id 始终相同,只需用该 id 覆盖旧条目。
我将非常感谢任何形式的帮助。我在几个小时内都在为此苦苦挣扎......
更新
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateGeschichteTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('geschichte', function (Blueprint $table) {
$table->increments('id');
$table->text('geschichte_text1');
$table->text('geschichte_text2');
$table->text('geschichte_text3');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('geschichte');
}
}
采纳答案by Alex Harris
Most of what is in your controller should not be necessary. I am also a little concerned about the database structure you are using as to why you would perform a task like shown in your controller. The following should be all you need:
您的控制器中的大部分内容都不是必需的。我也有点担心您使用的数据库结构,为什么要执行控制器中显示的任务。您只需要以下内容:
public function alterGeschichte(Request $request)
{
Geschichte::updateOrCreate(
['id' => 1],
['id' => 1, 'geschichte_text1' => $request->geschichte_text1]
);
Geschichte::updateOrCreate(
['id' => 2],
['id' => 2, 'geschichte_text2' => $request->geschichte_text2]
);
Geschichte::updateOrCreate(
['id' => 3],
['id' => 3, 'geschichte_text3' => $request->geschichte_text3]
);
return redirect('/geschichte');
}
If these are creating new records it is most likely because there is no record with those ID's.
如果这些正在创建新记录,很可能是因为没有这些 ID 的记录。
回答by user1669496
Looks like you don't have a unique key setup on the columns you wish to be unique. In order to use on duplicate key update
, you will need that so your database server will know if it needs to insert or update.
看起来您在希望唯一的列上没有唯一的键设置。为了使用on duplicate key update
,您将需要它,以便您的数据库服务器知道它是否需要插入或更新。
Unfortunately, Laravel doesn't have support for on duplicate key update
syntax. This would be useful because it tries to insert and if it's already in the table, then it will update the columns accordingly in one query.
不幸的是,Laravel 不支持on duplicate key update
语法。这将很有用,因为它会尝试插入,如果它已经在表中,那么它将在一个查询中相应地更新列。
Using Laravel's updateOrCreate
method, it first queries the table to see if it needs to generate an insert or update statement and then proceeds accordingly. You don't get the advantage of running just one query but at the same time, it's Laravel which is handling all the extra logic so it's a slight trade-off.
使用 Laravel 的updateOrCreate
方法,它首先查询表,看是否需要生成插入或更新语句,然后相应地进行。您不会获得仅运行一个查询的优势,但同时,Laravel 正在处理所有额外的逻辑,因此这是一个轻微的权衡。