Laravel stores a dynamic input data array into a database

I am trying to maintain a dynamic form in laravel. Failed to save the data array in the database. following form. enter image description here

normal form fields

{!! Form::select('customer_id', ['' => 'Select a customer'] + $customer_list ,null , array('class' => 'form-control', 'id' => 'customer')) !!} {!! Form::text('mileage_in', null, ['class' => 'form-control', 'placeholder' => 'Mileage In']) !!} . . .etc... 

Dynamic form fields

 {!! Form::select('item_id[][item_id]', $items, null, array('class' => 'form-control')) !!} {!! Form::text('item_description[][item_description]', null, ['class' => 'form-control', 'placeholder' => 'Not Required | Optional']) !!} {!! Form::text('units[][units]', null, ['class' => 'form-control', 'placeholder' => 'Add Units']) !!} {!! Form::text('rate[][rate]', null, ['class' => 'form-control', 'placeholder' => 'Add Rate']) !!} {!! Form::text('amount[][amount]', null, ['class' => 'form-control', 'placeholder' => 'Add Hrs and Rate', 'id' => 'amount']) !!} 

all these fields have the same shape. I save this data in three different tables.

follow the patterns i created.
Evaluating the scorecard model

 class Estimate extends Model { protected $fillable = [ 'customer_id', 'vehicle_id', 'mileage_in', 'net_amount', 'parent_estimate_id', 'department', 'created_by' ]; public function customer(){ return $this->belongsTo('App\Customer'); } public function vehicle(){ return $this->belongsTo('App\Vehicle'); } public function estimate_details(){ return $this->hasMany('App\EstimateDetail'); } } 

EstimateDetail model for valu_details table

 class EstimateDetail extends Model { protected $fillable = [ 'estimate_id', 'item_id', 'item_description', 'units', 'rate', 'labor_amount_final', 'initial_amount', 'task_status' ]; public function item() { return $this->hasMany('App\Item'); } public function estimate() { return $this->belongsTo('App\Estimate'); } } 

Item Model for Item Table

 class Item extends Model { protected $fillable = [ 'name', 'type', 'location', 'quantity', 'sale_price', 'unit_of_sale', 'pre_order_level', 'created_by', 'category_id', 'service_only_cost', 'updated_at' ]; public function estimate_details() { return $this->hasMany('App\EstimateDetail'); } } 

The following code is an EstimatesController

 class EstimatesController extends Controller { public function store(EstimateRequest $request) { $user_id = '1'; $input = $request->all(); $vehicle = new Vehicle(); $vehicle->customer_id = $input['customer_id']; $vehicle->reg_no = $input['reg_no']; $vehicle->make = $input['make']; $vehicle->model = $input['model']; $vehicle->created_by = $user_id; $estimate = new Estimate(); $estimate->customer_id = $input['customer_id']; $estimate->vehicle_id = $input['vehicle_id']; $estimate->mileage_in = $input['mileage_in']; $estimate->department = $input['department']; $estimate->created_by = $user_id; $estimate_detail = new EstimateDetail(); $estimate_detail->item_id = $input['item_id']; $estimate_detail->item_description = $input['item_description']; $estimate_detail->units = $input['units']; $estimate_detail->rate = $input['rate']; $estimate_detail->initial_amount = $input['amount']; $vehicle->save($request->all()); $vehicle->estimate()->save($estimate); $estimate->estimate_details()->save($estimate_detail); } return redirect('/estimates'); } 

I can successfully save a car and evaluate the data on the vehicle tables and ratings. but I cannot save arrays of evaluation data in the valu_details table. I tried many different ways, but finally could not. please, help!!!

+7
arrays php mysql model-view-controller laravel
source share
2 answers

I think id rating is missing. And also you cannot insert such arrays. Try this for a single item:

  $vehicle->save($request->all()); $vehicle->estimate()->save($estimate); $estimate_detail->estimate_id = $estimate->id; $estimate->estimate_details()->save($estimate_detail); 

Hope this helps.

+3
source share

To save an estimate detail from multiple lines, you can use the following approach.

// Prepare an array of all data using for loop

 $data = []; for($i=0,$i<count($input['item_id']);$i++) { $data[]= array ('item_id'=>$input[$i]['item_id'], 'item_description'=>$input[$i]['item_description']); 'units'=>$input[$i]['units']); 'rate'=>$input[$i]['rate']); 'initial_amount'=>$input[$i]['initial_amount']); } Model::insert($data); // Eloquent DB::table('table')->insert($data); // Query Builder 
0
source share

All Articles