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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 16:46:33  来源:igfitidea点击:

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

phpmysqllaravellaravel-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_detailstable product_id& qrcode_idinto winnerstable. How can i do that with query in Laravel? I have made a SQL Fiddle here.Thanks in advance.

现在,我想qr_detailsproduct_idqrcode_idwinners表。我如何在 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 winnerstable then you could use Eloquentmodels and insertmethod 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_idand qrcode_idto be added - in other words to update existing records.

但是,根据您所说的猜测,这可能不是您所追求的 - 因为您只想要product_id并被qrcode_id添加 - 换句话说,更新现有记录。

If that's the case, and if your idcolumn 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 Eloquentmodels - 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 winnerstable product_idand qrcode_idis NOT NULLso 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 NULLso that initially they don't require any data.

现在,有一个问题与你的SQL结构是你的winners桌子product_id,并qrcode_idNOT NULL因此它必须有一些数据出现在第一次创建记录时。如果您要更新这些记录,我建议将这两列更改为NULL最初它们不需要任何数据。