Reputation: 2735
I have a two tables:
qr_details table:
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.
Upvotes: 2
Views: 13042
Reputation: 1755
i know this late but someone looking for solution you can use chunk
laravel function. your tables have a large number of records, consider using a batch processing approach to avoid memory issues.
Table1::chunk(200, function ($table1Data) {
foreach ($table1Data as $record) {
// Create and save records in table2
}
});
Upvotes: 1
Reputation: 971
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
Upvotes: 3
Reputation: 6055
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.
$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.
If that's the case, and if your id
column matches in both of the tables then you could do something similar to:
$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
:
$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.
Upvotes: 0
Reputation: 7656
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])
}
Upvotes: 2