Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel saving dynamic input data array in to database

I'm trying to save a dynamic form in laravel. unable to save data array in to data base. following is the form. enter image description here

normal form fields in short

{!! 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 those fields are in same form. I'm saving these data into three different tables.

following are the models i created.
Estimate model for estimates table

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 estimate_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 items 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');
    }
}

Following code is the 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 vehicle and estimate data on tables vehicles and estimates. but i'm unable to save estimate details arrays on estimate_details table. tried many different ways but failed at last.

like image 833
Dhan Avatar asked Jan 28 '16 05:01

Dhan


Video Answer


1 Answers

I guess estimate id is missing. And also you cannot insert arrays like this. Try this for single item:

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

Hope it may helps.

like image 121
Md. Mahmud Hasan Avatar answered Nov 15 '22 17:11

Md. Mahmud Hasan