问题
My goal is to insert many records at the same time with the same form. I already created a function in the form element that will duplicate all the fields and pass all the data with one submit button. So theres no problem with the view and the data being passed to the controller.
I have an array of:
array:8 [▼
"_token" => "XLQVP4Hbm85SlZDFa6OnjK0LCoMOsrfs8jGCUwMj"
"id" => null
"client_id" => array:2 [▼
0 => "1"
1 => "1"
]
"sample_code" => array:2 [▼
0 => "sadasdas"
1 => "qwewqewqeqweq"
]
"sample_description" => array:2 [▼
0 => "dasdsad"
1 => "dsadsadasd"
]
"quantity" => array:2 [▶]
"analysis_requested" => array:2 [▼
0 => "dasdsadasd"
1 => "dsadsadas"
]
"special_instruction" => array:2 [▼
0 => "asdadasda"
1 => "asdasdaada"
]
]
T he given array above is just a sample data that I am passing from my form. This could be 3, 4 and so on depends on the user entry.
How will I insert this query using bulk insert?
So here is my code in the controller.
data = array(
array(
'client_id' => $input['client_id'][0],
'sample_code' => $input['sample_code'][0],
'sample_description' => $input['sample_description'][0],
'quantity' => $input['quantity'][0],
'analysis_requested' => $input['analysis_requested'][0],
'special_instruction' => $input['special_instruction'][0],
'status' => 'for_testing',
'created_at' => $date_today,
'updated_at' => $date_today,
),
array(
'client_id' => $input['client_id'][1],
'sample_code' => $input['sample_code'][1],
'sample_description' => $input['sample_description'][1],
'quantity' => $input['quantity'][1],
'analysis_requested' => $input['analysis_requested'][1],
'special_instruction' => $input['special_instruction'][1],
'status' => 'for_testing',
'created_at' => $date_today,
'updated_at' => $date_today,
),
);
AnalysisRequest::insert($data);
How will I simplified this code by using for each loop? and btw how will I get the last id of each data I inserted.
Appreciate if someone could help. Thanks in advance.
回答1:
I still think there is something wrong with what you are doing, but this may be a solution, although I do not think it is the best option
As I already told you, you should be receiving the data in the following way
array:2 [▼
"_token" => "XLQVP4Hbm85SlZDFa6OnjK0LCoMOsrfs8jGCUwMj"
"items" => array:2 [▼
0 => [▼
"client_id" => 1
"sample_code" => "aaa"
"sample_description" => "aaa"
"quantity" => array:2 [▶]
"analysis_requested" => "aaa"
"special_instruction" => "aaa"
]
1 => [▼
"client_id" => 1
"sample_code" => "bbb"
"sample_description" => "bbb"
"quantity" => array:2 [▶]
"analysis_requested" => "bbb"
"special_instruction" => "bbb"
]
]
]
In this way you can do
foreach($data as $item) { AnalysisRequest::create($item); }
But if you insist on doing in your way you has a lot of ways that is one
$newCollection = collect($input);
$datas = $collection->except(['_token', 'id']);
$items = [];
foreach($datas->all() as $field => $data) { // $field is field name
foreach($data as $key => $value) { // $key is item key
$item[$key][$field] = $value;
}
}
foreach($items as $item) { AnalysisRequest::create($item); }
I recommend you to see this video https://www.youtube.com/watch?v=Efr7SUrBUQw and refactor your form to obtain the data in the right way
来源:https://stackoverflow.com/questions/49348153/laravel-bulk-insert-many-entries-with-same-form