正在 Laravel 中保存来自动态表单的数据
Saving data from dynamic form in Laravel
我一直在尝试从 Laravel 5.3 中的动态表单保存数据。但我不能将它保存为数组。错误显示
Argument 1 passed to Illuminate\Database\Grammar::parameterize() must be of the type array, string given...
表格:
<select class="form-control-sm" name="client_id[]">
<input type="text" class="form-control-sm" name="amount[]">
型号:
protected $fillable = ['client_id', 'amount'];
public function client()
{
return $this->belongsTo('App\Client');
}
控制器:
public function store(Request $request)
{
$count = Client::count();
$payment = Payment::create(['amount' => $request->amount,
'client_id' => $request->client_id,
]);
$payment->save();
return redirect()->action('PaymentController@index');
}
请帮忙解决这个问题。谢谢。
我想像这样创建记录:
$payment = Payment::create($request->input);
并将您的重定向操作更改为:
View::make('path/to/view/')
或者只使用 back();
来测试它是否有效
you are submitting form with array of text fields and select box, try
below
public function store(Request $request)
{
$count = Client::count();
foreach( $request->client_id as $key=>$val){
$payment = Payment::create(['amount' => $request->amount[$key],
'client_id' => $val,
]);
}
return redirect()->action('PaymentController@index');
}
我一直在尝试从 Laravel 5.3 中的动态表单保存数据。但我不能将它保存为数组。错误显示
Argument 1 passed to Illuminate\Database\Grammar::parameterize() must be of the type array, string given...
表格:
<select class="form-control-sm" name="client_id[]">
<input type="text" class="form-control-sm" name="amount[]">
型号:
protected $fillable = ['client_id', 'amount'];
public function client()
{
return $this->belongsTo('App\Client');
}
控制器:
public function store(Request $request)
{
$count = Client::count();
$payment = Payment::create(['amount' => $request->amount,
'client_id' => $request->client_id,
]);
$payment->save();
return redirect()->action('PaymentController@index');
}
请帮忙解决这个问题。谢谢。
我想像这样创建记录:
$payment = Payment::create($request->input);
并将您的重定向操作更改为:
View::make('path/to/view/')
或者只使用 back();
来测试它是否有效
you are submitting form with array of text fields and select box, try below
public function store(Request $request)
{
$count = Client::count();
foreach( $request->client_id as $key=>$val){
$payment = Payment::create(['amount' => $request->amount[$key],
'client_id' => $val,
]);
}
return redirect()->action('PaymentController@index');
}