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.