Laravel将动态输入数据数组保存到数据库中

Dha*_*aya 10 php mysql arrays laravel

我正试图在laravel中保存一个动态表单.无法将数据阵列保存到数据库中.以下是表格. 在此输入图像描述

简而言之

{!! 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...
Run Code Online (Sandbox Code Playgroud)

动态表单字段

{!! 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']) !!}
Run Code Online (Sandbox Code Playgroud)

所有这些字段都是相同的形式.我将这些数据保存到三个不同的表中.

以下是我创建的模型.
估算表的估算模型

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');
    }
}
Run Code Online (Sandbox Code Playgroud)

估计_details表的EstimateDetail模型

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');
    }
}
Run Code Online (Sandbox Code Playgroud)

项目表的项目模型

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');
    }
}
Run Code Online (Sandbox Code Playgroud)

以下代码是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');
  }
Run Code Online (Sandbox Code Playgroud)

我可以成功地保存车辆并估算表格车辆和估算数据.但是我无法在estimate_details表上保存估计详细信息数组.尝试了许多不同的方法,但最后失败了.

Md.*_*san 5

我估计缺少ID。而且,您不能插入这样的数组。尝试以下单个项目:

    $vehicle->save($request->all());
    $vehicle->estimate()->save($estimate);
    $estimate_detail->estimate_id = $estimate->id;
    $estimate->estimate_details()->save($estimate_detail);
Run Code Online (Sandbox Code Playgroud)

希望它会有所帮助。