我正在尝试将从Post表单动态输入的数据存储到Laravel中的MySQL数据库中。
购买模式
命名空间应用;
使用Illuminate \ Database \ Eloquent \ Model;
类购买扩展了模型 { 受保护的$ fillable = ['clid,date,name,quantity,price,subTotal,grandtotal'];
public function setClideAttribute($input){
$this->attributes['clid'] = $input ? $input : null;
}
public function setDateAttribute($input){
$this->attributes['date'] = $input ? $input : null;
}
public function setNameAttribute($input){
$this->attributes['name'] = $input ? $input : null;
}
public function setQuantityAttribute($input){
$this->attributes['quantity'] = $input ? $input : null;
}
public function setPriceAttribute($input){
$this->attributes['price'] = $input ? $input : null;
}
public function setSubtotalAttribute($input){
$this->attributes['subTotal'] = $input ? $input : null;
}
public function setGrandtotalAttribute($input){
$this->attributes['grandtotal'] = $input ? $input : null;
}
}
PurchasesController
公共 函数库(请求$ request){
$this - > validate(request(), [
'date' => 'required',
'name' => 'required',
'quantity' => 'required',
'price' => 'required',
'subTotal' => 'required',
'grandtotal' => 'required'
]);
foreach($request - > get('purchases_details') as $Purchase) {
$Purchase = new Purchase([
'date' => $request - > get('date'),
'name' => $request - > get('name'),
'quantity' => $request - > get('quantity'),
'price' => $request - > get('price'),
'subtotal' => $request - > get('subtotal'),
'grandtotal' => $request - > get('grandtotal')
]);
}
$purchase - > save();
return redirect() - > route('purchase.store') - > with('success', 'Data Added');
DB::table('purchases') - > insert($data);
echo "success";
}
以表格形式动态输入的数据未存储在数据库中