laravel 如何将另一个表数据插入另一个表laravel 5.2?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46659774/
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
How to insert another table data into another table laravel 5.2?
提问by Chonchol Mahmud
I have a two tables:
我有两个表:
qr_details table:
qr_details 表:
id product_id qrcode_id created_at updated_at
1 1 12 2017-10-09 15:36:15 2017-10-09 15:36:15
2 3 13 2017-10-09 15:36:15 2017-10-09 15:36:15
winners table:
获奖表:
id product_id qrcode_id winner_name win_number created_at updated_at
1 1 12 hello 5 2017-10-09 15:36:15 2017-10-09 15:36:15
2 3 13 world 6 2017-10-09 15:36:15 2017-10-09 15:36:15
Now i want to get qr_details
table product_id
& qrcode_id
into winners
table. How can i do that with query in Laravel? I have made a SQL Fiddle here.Thanks in advance.
现在,我想qr_details
表product_id
和qrcode_id
成winners
表。我如何在 Laravel 中使用查询来做到这一点?我在这里做了一个 SQL Fiddle 。提前致谢。
回答by ssuhat
I don't really understand your question but you can try this:
我不太明白你的问题,但你可以试试这个:
$datas = DB::table('qr_details ')->get();
foreach($datas as $data){
DB::table('winners')->insert(['qrcode_id' => $data->qrcode_id, 'product_id'=>$data->product_id, ...bunch other inserts])
}
回答by TheGeeky
I believe you can do something like this:
我相信你可以做这样的事情:
$query = \DB::connection()->getPdo()->query("select * from qr_details");
$data = $query->fetchAll(\PDO::FETCH_ASSOC);
\DB::table('winners')->insert($data);
it will take a little time and just two queries
这将需要一点时间和只有两个查询
回答by Sebastian Sulinski
If you were to add new records to the winners
table then you could use Eloquent
models and insert
method to add multiple record in a single query.
如果要向winners
表中添加新记录,则可以使用Eloquent
模型和insert
方法在单个查询中添加多条记录。
$qcodes = Qrcode::all()->map(function(Qrcode $qrcode) {
return [
'id' => $qrcode->id,
'product_id' => $qrcode->product_id,
'qrcode_id' => $qrcode->qrcode_id,
'winner_name' => 'some name',
'win_number' => 5
];
});
Winner::insert($qcodes);
However, guessing from what you said, that's probably not what you're after - as you want only product_id
and qrcode_id
to be added - in other words to update existing records.
但是,根据您所说的猜测,这可能不是您所追求的 - 因为您只想要product_id
并被qrcode_id
添加 - 换句话说,更新现有记录。
If that's the case, and if your id
column matches in both of the tables then you could do something similar to:
如果是这种情况,并且您的id
列在两个表中都匹配,那么您可以执行以下操作:
$qcodes = Qrcode::all();
$qcodes->each(function(Qrcode $qrcode) {
Winner::where('id', $qrcode->id)->update([
'product_id' => $qrcode->product_id,
'qrcode_id' => $qrcode->qrcode_id
]);
});
This is again assuming you are using Eloquent
models - otherwise you'd have to do it using Query Builder
:
这再次假设您正在使用Eloquent
模型 - 否则您必须使用Query Builder
:
$qcodes= DB::table('qr_details')->get();
$qcodes->each(function(Qrcode $qrcode) {
DB::table('winners')
->where('id', $qrcode->id)
->update([
'product_id' => $qrcode->product_id,
'qrcode_id' => $qrcode->qrcode_id
]);
});
Make sure you update table / model names accordingly.
确保相应地更新表/模型名称。
Now, one issue with your sql structure is that your winners
table product_id
and qrcode_id
is NOT NULL
so it has to have some data there when record is first created. If you were to update these records, I would suggest to change these two columns to NULL
so that initially they don't require any data.
现在,有一个问题与你的SQL结构是你的winners
桌子product_id
,并qrcode_id
为NOT NULL
因此它必须有一些数据出现在第一次创建记录时。如果您要更新这些记录,我建议将这两列更改为NULL
最初它们不需要任何数据。