I have the array as below;
I’d like to insert each name
keys into tableName
and get the inserted id.
For the steps
, each of them will be inserted into another table tableSteps
including the last inserted id of the name
.
Like as below screenshot.
In my controller,
Here’s what I’ve done so far.
$instructionsArrays = $request->instructions;
$max = count($instructionsArrays);
for ($x = 1; $x <= $max; $x++) {
foreach($instructionsArrays as $instructionsArray){
Instruction::updateOrCreate(
['recipe_id' => session()->get('recipeArr.id'), 'sequence' => $x],
['name' => $instructionsArray['name']],
);
}
}
I was able to save sequence numbers but for names it saves only the last name
key.
And… I’m really lost..
2
Answers
With the help of the answer of @Kneegrows, I came up with the code below and it is now working. Thank you.
You can achieve what you want from 2 for loops