11

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.

2 Answers 2

5

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Thank you for your reply. I tried this before. but i'm getting this error. "preg_replace(): Parameter mismatch, pattern is a string while replacement is an array"
Yes its because of you are trying to insert array at once. add an index after your items like $estimate_detail->item_id = $input['item_id'][0]; $estimate_detail->item_description = $input['item_description'][0]; $estimate_detail->units = $input['units'][0]; $estimate_detail->rate = $input['rate'][0]; $estimate_detail->initial_amount = $input['amount'][0]; if it works then go for a loop.
i tried adding indexes without a loop. still getting same error. "preg_replace(): Parameter mismatch . . . " and detailed error as following"'insert into estimate_details (estimate_id, item_id, item_description, units, rate, initial_amount, updated_at, created_at) values (89, ?, ?, ?, ?, ?, ?, ?)"
Chamge your html. remove last index like Form::select('item_id[][item_id]' => Form::select('item_id[]' item_description[][item_description] => item_description[]
Cheers! Now do it inside a loop without indexing.
0

To store estimate detail in from multiple rows, You can use the following approach.

// Prepare 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

2 Comments

Thank you for your reply. i tried with both Eloquent and Query Builder "EstimateDetail::insert($data);" and "DB::table('estimate_details')->insert($data);" But i'm getting "Undefined offset: 0". any idea why this happening.
Before Insert, Please verify your $data array is not null and check that array creation loop. Main think is to generate proper array from input data.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.