0

I have the array as below;

enter image description here

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.

enter image description here

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..

8
  • why are u using update or create function? Im trying to solve this for you but the part Im missing is why are u using that Commented Dec 1, 2022 at 11:21
  • You ever think about that this happens because all your test values are Some value ? Commented Dec 1, 2022 at 11:25
  • @geertjanknapen I was gonna say that but Im still waiting for him to explain xd Commented Dec 1, 2022 at 11:27
  • Yeah the updateOrCreate does not really make sense, but it's not really the question being asked.. Commented Dec 1, 2022 at 11:28
  • @Kneegrows, I'm using updateOrCreate if incase it doesn't exist for that particular Recipe_ID. I can use create or whatever but I have an issue with such. Commented Dec 1, 2022 at 11:30

2 Answers 2

2

You can achieve what you want from 2 for loops

foreach($request->instructions as $key => $val){
$id = Instruction::insertGetId(
    ['recipe_id' => session()->get('recipeArr.id'), 'sequence' => $key + 1],
    ['name' => $val['name']],
);
$data = []; //bulk insertion
$created_at = now();
foreach($val["steps"] as $step){
    array_push($data, ["header_id" => $id, "name" => $step, "sequence" => $key+1, "created_at" => $created_at]); //why insert sequence when you can obtain it from the relationship?
  }
  Steps::insert($data);
 }
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you. But getting this error. SQLSTATE[HY000]: General error: 1364 Field 'name' doesn't have a default value
But when I dd($val['name']), it shows the name. :'(
That happens if you don't have fillable attributes in your model. protected $fillable = ['sequence', 'name', 'header_id'];
It has name in the Model. I played around and I was thinking there were 2 name columns in both tables. So what I did is rename it to instructions_name and the other is steps_name and yes, it worked. Anyway, your suggestion gave me an idea of how can I solve my problem. I will post the correct code next. I marked your answer as very useful :). Thank you very much.
2

With the help of the answer of @Kneegrows, I came up with the code below and it is now working. Thank you.

        foreach ($request->instructions as $key => $val) {
         $instruction = Instruction::updateOrCreate(
                ['recipe_id' => session()->get('recipeArr.id'), 'sequence' => $key + 1],
                ['instructions_name' => $val['name']],
            );
        $id = $instruction->id;
        $data = []; //bulk insertion
        $i = 1;
        foreach ($val["steps"] as $step) {
            if(!is_null($step)){
                array_push($data, ["instruction_id" => $id, "steps_name" => $step, "sequence" => $i]);
                $i++;
            }
        }
        Steps::insert($data);
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.