1

I'm using jquery nested form repeater. When I submit form I get these values.

Illuminate\Http\Request {#51 ▼
  +request: Symfony\Component\HttpFoundation\ParameterBag {#52 ▼
    #parameters: array:22 [▼
      "_token" => "brgVhd3F1K0wWqxsjDn6Vf9gBdKLX8CO8XbpkBJJ"
      "options" => array:2 [▼
        0 => array:4 [▼
          "name" => "Color"
          "type" => "dropdown"
          "is_required" => array:1 [▶]
          "values" => array:1 [▼
            0 => array:3 [▼
              "label" => "Red"
              "price" => "100"
              "price_type" => "fixed"
            ]
          ]
        ]
        1 => array:3 [▼
          "name" => "Size"
          "type" => "dropdown"
          "values" => array:1 [▼
            0 => array:3 [▼
              "label" => "s"
              "price" => "100"
              "price_type" => "percent"
            ]
          ]
        ]
      ]
    ]
  }
}

I have tried using this. But it's give me Trying to get property 'name' of non-object;

foreach ($request->input('options') as $option) {

    Option::create([

        'name' => $option->name,

        'type' => $option->type,

         'is_required' => (boolean) $option->is_required
    ]);


     $option->values()->createMany($request->input('options.*.values'));

}

How can I access values?

Please help me to solve this problem.

Thanks.

12
  • I am using this $request->input('options.*.values'); Commented Jan 11, 2020 at 11:36
  • Wait Please.. I am updating my question form more spacefic. Commented Jan 11, 2020 at 11:37
  • Please ca you show the output of dd($request->all());. Commented Jan 11, 2020 at 11:44
  • My form is too long so show my some part using dd Commented Jan 11, 2020 at 12:21
  • What is the problem And where is my mistake? Commented Jan 11, 2020 at 12:24

2 Answers 2

2

The reason you're having this issue is because $request->input() will return an array not objects so for the most part you were just trying to access the property in the wrong way i.e. $options->name should have been $options['name'].

Secondly, $option->values()->createMany($request->input('options.*.values')); wouldn't have worked either as it would have returned a nested array of all the values and not just the ones for that option.

The following should get you what you're after:

foreach ($request->input('options') as $option) {

    $option = Option::create([
        'name'        => $option['name'],
        'type'        => $option['type'],
        'is_required' => (boolean)$option['is_required'],
    ]);

    $option->values()->createMany($option['values']);
}

This is just an FYI.

I would recommend using the $casts property inside your Eloquent model if you're not already:

protected $casts = [
    'is_required' => 'boolean',
];

This way you can tell Eloquent that is_required is meant to be a boolean and you then won't have to cast it yourself so in your foreach loop

'is_required' => (boolean)$option['is_required'],  

would just be

'is_required' => $option['is_required'],
Sign up to request clarification or add additional context in comments.

14 Comments

Thanks. It's Working. But What if I want to get direct from request without input?
@ShahadatHossain I'm not sure what you mean since input is from the request. Please may you explain in more detail.
I mean there have any other way without this $request->input('options'). Like $request->something
@ShahadatHossain You could use $request->options.
I have another thing to know... without loop through have any other way to insert to my Option model?
|
1

You can try the following method

foreach ($request['options'] as $option) {
    Option::create([
        'name' => $option->name,
        'type' => $option->type,
         'is_required' => (boolean) $option->is_required
    ]);
    $option->values()->createMany($request->input('options.*.values'));
}

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.