Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在Laravel 6+;中的insert()函数后检索批量插入的ID;_Php_Mysql_Laravel_Laravel 6_Laravel 6.2 - Fatal编程技术网

Php 在Laravel 6+;中的insert()函数后检索批量插入的ID;

Php 在Laravel 6+;中的insert()函数后检索批量插入的ID;,php,mysql,laravel,laravel-6,laravel-6.2,Php,Mysql,Laravel,Laravel 6,Laravel 6.2,我做了很多研究,但没有找到任何解决办法。所以我把它贴在这里 我的问题是,在插入大量行之后,我希望获得所有插入的ID,以便将ID保存到另一个透视表中。。这是我的密码 $create_market = []; $create_market_possibility = []; foreach ($request->type0 as $key => $value) { array_push

我做了很多研究,但没有找到任何解决办法。所以我把它贴在这里 我的问题是,在插入大量行之后,我希望获得所有插入的ID,以便将ID保存到另一个透视表中。。这是我的密码

$create_market = [];
                $create_market_possibility = [];
                foreach ($request->type0 as $key => $value) {
                    array_push($create_market, [
                        'market_id' => $value['market_id'],
                        'event_id' => $value['event_id'],
                        'name' => $value['name'],
                        'bet_min' => $value['min_bet'],
                        'bet_max' => $value['max_bet'],
                        'commission' => $value['commission'],
                        'type' => 0,
                        'created_at' => \Carbon\Carbon::now(),
                        'updated_at' => \Carbon\Carbon::now()
                    ]);
                }
                foreach ($request->type1 as $key => $value1) {
                    array_push($create_market, [
                        'market_id' => $value1['market_id'],
                        'event_id' => $value1['event_id'],
                        'name' => $value1['name'],
                        'bet_min' => $value1['min_bet'],
                        'bet_max' => $value1['max_bet'],
                        'commission' => $value1['commission'],
                        'type' => 1,
                        'created_at' => \Carbon\Carbon::now(),
                        'updated_at' => \Carbon\Carbon::now()
                    ]);
                    foreach ($value1['possibility'] as $key => $value2) {
                        array_push($create_market_possibility, [

                            // because i am not getting the inserted ids here i cant push it here
                            // that is the problem i am facing 

                            'market_id' => $value1['market_id'],
                            'event_id' => $value1['event_id'],
                            'possibility' => $value2['possibility'],
                            'created_at' => \Carbon\Carbon::now(),
                            'updated_at' => \Carbon\Carbon::now()
                        ]);
                    }
                }
                Market::insert($create_market);
                // Here i want to retrive the last inserted all ids and put then in the obj of
                  [$create_market_possibility] array ... 

                if(count($create_market_possibility) > 0) {
                    MarketPossibility::insert($create_market_possibility);
                }
                $response = [
                    'status' => true,
                    'message' => 'Market Successfully Created'
                ];
                return response()->json($response); //@ sending response

我在使用create()进行单次插入时做了这件事
$id=Market::create($array)
它把东西还给了我。。但在这种情况下,我必须插入多行。
如果有其他方法,请告诉我,谢谢

一,。对于type0 您可以为type0运行Market::insert($array),因为没有关联的MarketObability

2.对于类型1, 您必须逐个创建每个市场,然后将其关联起来-您可以使用
saveMany()
使其更干净、更快:

$market = Market::create([...])

$new_market_possibility = [];
foreach ($value1['possibility'] as $key => $value2) {
    $new_market_possibility[] = new App\MarketPossibility([...]);
}

$market->marketPossibilities()->saveMany($new_market_possibilities);


所有这些假设您在市场和市场可能性之间有标准关系

您是否尝试过
$create\u Market\u probability=Market::insert($create\u Market)
您说的是在create\u Market\u probability数组中保存对象??。。。我尝试过这个,但它返回我1`$market=market::create([…])`将适用于多次插入??因为请求数据是一个数组,所以您必须针对每个市场运行关联,所以z market也只是针对单个市场插入多个。。然后,我必须在一个循环下运行查询,我不是在寻找好的,我不确定你能在一个命令中实现你想要的,因为你无法从你的市场获得ID,这就是问题所在。。有没有办法在insert()方法之后获取ID