Laravel 将动态输入数据数组保存到数据库中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35053656/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel saving dynamic input data array in to database
提问by Dhananjaya
I'm trying to save a dynamic form in laravel. unable to save data array in to data base. following is the form.
我正在尝试在 Laravel 中保存动态表单。无法将数据数组保存到数据库中。以下是表格。
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
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');
}
}
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
以下代码是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.
我可以成功地保存车辆和估算表格车辆和估算的数据。但我无法在estimate_details 表上保存估计详细信息数组。尝试了很多不同的方法,但最后都失败了。
采纳答案by Md. Mahmud Hasan
I guess estimate id is missing. And also you cannot insert arrays like this. Try this for single item:
我猜估计 id 丢失了。而且你不能像这样插入数组。对单个项目试试这个:
$vehicle->save($request->all());
$vehicle->estimate()->save($estimate);
$estimate_detail->estimate_id = $estimate->id;
$estimate->estimate_details()->save($estimate_detail);
Hope it may helps.
希望它可以帮助。
回答by Ali
To store estimate detail
in from multiple rows, You can use the following approach.
要从estimate detail
多行存储,您可以使用以下方法。
// Prepare array of all data using for loop
// 使用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