I have an edit form for a model and a section is dedicated to that specific record's child records called "shipment_details".
Editing a record will always show at least one existing shipment_detail record. At the moment my HTML of the "shipment_details" is as such from the Blade:
<tbody>
@foreach($shipment_details as $sd)
<tr style="height:40px">
<td style="width:8%;text-align:center;">{{Form::text('shipment_details['.$sd->id.'][piecesNumber]', $sd->pieces_number, array('class' => 'form-control','placeholder'=>'No. Pieces','required','id'=>'piecesNumber'))}}
</td>
<td style="width:16%;text-align:center;">
{!! Form::select('shipment_details['.$sd->id.'][piecesType]', $piecetypes, $sd->pieces_type, ['id' => 'pieces_type', 'class' => 'form-control full-width','required']) !!}
</td>
<td>
{!! Form::select('shipment_details['.$sd->id.'][rateType]', $ratetypes, $sd->rate_type, ['id' => 'rateType', 'class' => 'form-control full-width','required']) !!}
</td>
<td style="width:16.5%;text-align:center;">
{{Form::text('shipment_details['.$sd->id.'][weight]', $sd->weight, array('class' => 'form-control','placeholder'=>'Weight','required','id'=>'weight'))}}
</td>
<td style="width:16.5%;text-align:center;">
{{Form::select('shipment_details['.$sd->id.'][hazmat]',array(
'0'=>'No',
'1'=>'Yes',
), $sd->hazmat, array('class' => 'form-control','id'=>'hazmat'))}}
</td>
<td style="width:16.5%;text-align:center;">
{{Form::text('shipment_details['.$sd->id.'][description]', $sd->description, array('class' => 'form-control','placeholder'=>'Description','required','id'=>'description'))}}
</td>
<td style="width:16.5%;text-align:center;">
{{Form::text('shipment_details['.$sd->id.'][charge]', $sd->charge, array('class' => 'form-control','placeholder'=>'Charge','required','id'=>'charge'))}}
</td>
<td><button type="button" name="add" id="add" class="btn btn-success">Add More</button></td>
</tr>
@endforeach
</tbody>
But if there is a need to add additional shipment_details, I have a script which adds additional record rows formatted below:
<script>
$(document).ready(function(){
var i=1;
$('#add').click(function(){
i++;
$('#freight_bill_items').append('<tr id="row'+i+'"> <td style="width:8%;text-align:center;">{{Form::text('shipment_details[piecesNumber][]', null, array('class' => 'form-control','placeholder'=>'No. Pieces','required','id'=>'piecesNumber'))}}</td><td style="width:16%;text-align:center;">{!! Form::select('shipment_details[piecesType][]', $piecetypes, 'null', ['id' => 'pieces_type', 'class' => 'form-control full-width','required']) !!} </td><td>{!! Form::select('shipment_details[rateType][]', $ratetypes, null, ['id' => 'rateType', 'class' => 'form-control full-width','required']) !!}</td><td style="width:16.5%;text-align:center;">{{Form::text('shipment_details[weight][]', null, array('class' => 'form-control','placeholder'=>'Weight','required','id'=>'weight'))}}</td><td style="width:16.5%;text-align:center;">{{Form::select('shipment_details[hazmat][]',array('No'=>'No','Yes'=>'Yes',), null, array('class' => 'form-control','id'=>'hazmat'))}}</td><td style="width:16.5%;text-align:center;">{{Form::text('shipment_details[description][]', null, array('class' => 'form-control','placeholder'=>'Description','required','id'=>'description'))}}</td><td style="width:16.5%;text-align:center;">{{Form::text('shipment_details[charge][]', null, array('class' => 'form-control','placeholder'=>'Charge','required','id'=>'charge'))}}</td><td><button type="button" name="remove" id="'+i+'" class="btn btn-danger btn_remove">X</button></td></tr>');
});
$(document).on('click', '.btn_remove', function(){
var button_id = $(this).attr("id");
$('#row'+button_id+'').remove();
$( 'input[name^="shipment_details[charge][]"]' ).trigger( "change" );
});
});
</script>
My problem arises when I go to post the data, the existing data is "arrayed" correctly, but the new data isn't and at the moment gives me an undefined index record.
Here is the current POST data output for shipment_details for a test record:
shipment_details
array:8 [▼
13149 => array:7 [▼
"piecesNumber" => "1"
"piecesType" => "1"
"rateType" => "1"
"weight" => "12"
"hazmat" => "0"
"description" => "124"
"charge" => "12.00"
]
"piecesNumber" => array:1 [▼
0 => "3"
]
"piecesType" => array:1 [▼
0 => "2"
]
"rateType" => array:1 [▼
0 => "2"
]
"weight" => array:1 [▼
0 => "1200"
]
"hazmat" => array:1 [▼
0 => "Yes"
]
"description" => array:1 [▼
0 => "desc2"
]
"charge" => array:1 [▼
0 => "40.00"
]
]
So as you can see, the inputs are all separate except for the existing record, so my problem must be originating in my form. Does anyone have any suggestions?
Update
Using the above output:
This is how it should be sent (or in a similar format):
13149 => array:7 [▼
"piecesNumber" => "1"
"piecesType" => "1"
"rateType" => "1"
"weight" => "12"
"hazmat" => "0"
"description" => "124"
"charge" => "12.00"
]
Since the new records don't have a record ID assigned yet, I don't believe the top row would be the same for the new records but this should explain my request.
Update
Portion of Controller with Section about shipment_detail
foreach ($request->shipment_details as $id => $details) {
$shipdetail = Shipment_Detail::updateOrCreate(['id' => $id], [
'pieces_type' => $details['piecesType'][0],
'pieces_number' => $details['piecesNumber'][0],
'rate_type' => $details['rateType'][0],
'weight' => $details['weight'][0],
'charge' => $details['charge'][0],
'description' => $details['description'][0],
'hazmat' => $details['hazmat'][0]
]);
}
piecesNumberaskey => valuefor example and collect values from other sub-arrays under the samekey.