mardi 1 mars 2016

PHP / Laravel: How to save a multidimensional array into the DB

I have a large form where 3 arrays are sent to the controller from the form: product_code | quantity | cost.

 $id = $request->get('product_code');   // GRAB PRODUCT DATA FROM POST ARRAY
    $qty = $request->get('quantity');   // GRAB QUANTITY DATA FROM POST ARRAY
    $cost = $request->get('cost');      // GRAB COST DATA FROM POST      

My problem is I can not figure out how best to loop through each of these three arrays so that I can insert into MySQL the values in the correct order. I have tried both a foreach, a nested foreach and a for loop and I have not managed yet to get all three values inserting onto a single row.

I don't think the HTML is very relavent, but here is a sample anyway:

 <div class="well form-group  ">
  <div class="col-sm-2 col-md-2">{!! Form::label('nails_staples', 'Nails & Staples:') !!}</div>
  <div class="col-sm-4 col-md-4">{!! Form::select('product_code[10]',$nails_staples, null, ['class'=>'form-control product_id','placeholder'=>'None']) !!}</div>
  <div class="col-sm-1  col-md-1">{!! Form::label('nails_req', 'Quantity:') !!}</div>
   <div class="col-sm-2 col-md-2">{!! Form::text('quantity[10]',null,['class'=>'form-control  quantity']) !!}</div>
  <div class="col-sm-1  col-md-1">{!! Form::label('cost', 'Cost:') !!}</div>
  <div class="col-sm-2 col-md-2">{!! Form::text('cost[]',null ,['class'=>'form-control cost', 'readonly'=>'readonly' ]) !!}</div>
  </div>



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire