gpt4 book ai didi

php - 在 laravel 插入查询中,我想在数组上实现一个 for 循环以一次性插入多条记录

转载 作者:行者123 更新时间:2023-12-05 07:35:06 25 4
gpt4 key购买 nike

我想要实现的是,在数据库查询中,我一次性插入所有记录,这样查询就不会运行多次。下面的代码不允许我在数组中使用 for 循环。请帮忙。

if($user=="MasterDealer")
{
if($request['accessibleTodealers']!='' || $request['accessibleTodealers']!=null)
{
$memberships=explode(",",$request['accessibleTodealers']);
$membe = array();
$now = Carbon::now();
DB::table('accessible_membership_logs')->insert([
array(
for ($i=0; $i <count($memberships) ; $i++){
array(
['membership_id']=$memberships[$i],
['masterdealer_id']=$dealer,
['dealer_id'] = null,
['user'] = 'masterdealer',
['operation'] = 'Accessible',
['admin_id'] = $data['id'],
['created_at'] = $now->format('Y-m-d H:i:s'),
['updated_at'] = $now->format('Y-m-d H:i:s')
)}
)
]);
Alert::message("","Changes Updated.!","success");
return redirect()->intended('/admin/masterDealerDetail/'.$dealer);
}
}

最佳答案

插入查询之前做循环

$array = array();

for ($i=0; $i <count($memberships) ; $i++){
$array[] = array(
'membership_id' => $memberships[$i],
'masterdealer_id' => $dealer,
'dealer_id' => null,
'user' => 'masterdealer',
'operation' => 'Accessible',
'admin_id' => $data['id'],
'created_at' => $now->format('Y-m-d H:i:s'),
'updated_at' => $now->format('Y-m-d H:i:s')
);
}

DB::table('accessible_membership_logs')->insert($array);

关于php - 在 laravel 插入查询中,我想在数组上实现一个 for 循环以一次性插入多条记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49747768/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com