[英]How to insert another table data into another table laravel 5.2?
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.现在我想将qr_details
表product_id
和qrcode_id
放入winners
表中。 How can i do that with query in Laravel?我如何在 Laravel 中使用查询来做到这一点? I have made a SQL Fiddle here .我在这里做了一个 SQL Fiddle 。 Thanks in advance.提前致谢。
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这将需要一点时间和两个查询
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])
}
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.现在,您的 sql 结构的一个问题是您的winners
表product_id
和qrcode_id
NOT NULL
因此在第一次创建记录时它必须有一些数据。 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.如果您要更新这些记录,我建议将这两列更改为NULL
以便它们最初不需要任何数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.