繁体   English   中英

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

[英]Laravel saving dynamic input data array in to database

我正试图在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...

动态表单字段

{!! 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']) !!}

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

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

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

估计_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');
    }
}

项目表的项目模型

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

以下代码是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');
  }

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

我猜估计ID丢失了。 而且你也不能插入像这样的数组。 试试这个单品:

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

希望它可能有所帮助。

要从多行存储estimate detail ,您可以使用以下方法。

//使用for循环准备所有数据的数组

$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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM