mardi 23 juin 2020

Cannot get data from dynamic input fields using foreach loop in Laravel

I have multiple multiple input fields in my form that are being created dynamically, Now there are two sets one is attribute and the other is attribute values like this:

<input type="text" name="attribute_name[attr_1]" placeholder="Attribute Name" class="form-control" required="">

<input type="text" name="attribute_value[][attr_1]" placeholder="Attribute Value" class="form-control" required="">

<input type="text" name="attribute_value[][attr_1]" placeholder="Attribute Value" class="form-control" required="">

<input type="text" name="attribute_name[attr_2]" placeholder="Attribute Name" class="form-control" required="">

<input type="text" name="attribute_value[][attr_2]" placeholder="Attribute Value" class="form-control" required="">

<input type="text" name="attribute_value[][attr_2]" placeholder="Attribute Value" class="form-control" required="">

Now using foreach loop, first i want to get all the attribute fields, then in the inner loop i want to am getting the attribute values like in the code below:

foreach($request->input('attribute_name') as $attrKey => $val)
{
    foreach ($request->input('attribute_value.*.'.$attrKey)  as $attr_valKey => $value) {
        $attribute_value=$value;
    }
}

$request->input('attribute_name') contains Array ( [attr_1] => Color [attr_2] => Size )

The concept i'm using is i don't know the key because it is dynamic, so i'm first trying to loop through all the attribute name one by one and then in the inner loop extract attribute values.

For first time i'm getting the attribute and its related value for second attribute, i didn't get any value.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire