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

时间:2016-01-28 05:42:50

标签: php mysql arrays laravel

我正在尝试在laravel中保存动态表单。无法将数据阵列保存到数据库中。以下是表格。 enter image description here

简短的普通表单字段

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

estimate_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表上保存估计详细信息数组。尝试了许多不同的方法,但最后失败了。

2 个答案:

答案 0 :(得分:4)

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

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

希望它可能会有所帮助。

答案 1 :(得分:0)

要从多行存储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