简体   繁体   中英

How to insert another table data into another table laravel 5.2?

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM