Php Laravel将动态输入数据数组保存到数据库中
我正在尝试在laravel中保存一个动态表单。无法将数据数组保存到数据库中。以下是表格。 简言之为标准格式字段Php Laravel将动态输入数据数组保存到数据库中,php,mysql,arrays,laravel,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, ['clas
{!! 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');
}
}
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');
}
}
估算详细信息表的估算详细信息模型
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');
}
}
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');
}
}
项目表的项目模型
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');
}
}
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');
}
}
以下代码是估计控制器
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表上保存estimate details数组。尝试了许多不同的方法,但最终失败了 要从多行存储
估算详细信息
,可以使用以下方法
//使用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
$data=[];
对于($i=0,$i$input[$i]['item_id'],
'item_description'=>$input[$i]['item_description']);
“单位”=>$input[$i]['units']);
“速率”=>$input[$i]['rate']);
“初始金额”=>$input[$i][“初始金额”]);
}
模型::插入($data);//雄辩的
DB::table('table')->insert($data);//查询生成器
我想估计id丢失了。而且不能像这样插入数组。对于单个项目,请尝试以下操作:
$vehicle->save($request->all());
$vehicle->estimate()->save($estimate);
$estimate_detail->estimate_id = $estimate->id;
$estimate->estimate_details()->save($estimate_detail);
希望能有所帮助。谢谢您的回复。我尝试使用雄辩的和查询生成器“EstimateDetail::insert($data);”和“DB::table('estimate_details')->insert($data);”,但得到的是“未定义的偏移量:0”。知道为什么会发生这种情况吗?在插入之前,请验证您的
$data
数组是否为空,并检查数组创建循环。主要思想是从输入数据生成合适的数组。谢谢您的回复。我以前试过这个。但我得到了这个错误。“preg_replace():参数不匹配,模式为字符串,而替换为数组”是,因为您试图立即插入数组。在项目后添加索引,如$estimate\u detail->item\u id=$input['item\u id'][0]$估算详细信息->项目描述=$input['项目描述][0]$估算详细信息->单位=$input['units'][0]$估算详细信息->费率=$input['rate'][0]$估算详细信息->初始金额=$input['amount'][0];如果它有效,那么就进行循环。我尝试在没有循环的情况下添加索引。仍然得到相同的错误。“preg_replace():参数不匹配…”和如下详细错误“'insert inestimate_details
(estimate_id
,item_description
,units
,rate
,初始金额
,更新的
,创建的
)值(89,,,,,,,,,,,,,,”改变你的html。删除最后一个索引,如Form::select('item_id[][item_id]'=>Form::select('item_id[]'
item_description[]=>item_description[]
干杯!现在在循环中进行,不需要索引。