有人可以帮我吗?
我正在尝试从表单中获取数据并分为几个模型。我从表单中获取数组的情况有问题,并将数据添加到每个模型中的数据库中。
代码:
<div class="form-check form-check-success">
<label class="form-check-label">
{!! Form::checkbox('certification_id[]', '1', false, ['class' => 'form-check-input']) !!}
Data
</label>
</div>
<div class="form-check form-check-success">
<label class="form-check-label">
{!! Form::checkbox('certification_id[]', '2', false, ['class' => 'form-check-input']) !!}
Data
</label>
</div>
控制器代码:
public function store(CreateWaybillRequest $request) {
$waybill = new Waybill($request->all());
$certifications = new WaybillCertification($request->$_POST['certification_id']);
//$id = Auth::user()->waybills()->save($waybill)->id;
return print_r($certifications);
请求类:
namespace AppHttpRequests;
use IlluminateFoundationHttpFormRequest;
class CreateWaybillRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'weight' => 'required',
'fromDate' => 'required|date',
'damage' => 'required',
'fuelUsed' => 'required',
'toDate' => 'required|date',
'length' => 'required'
];
}
}
这是错误的 -
$certifications = new WaybillCertification($request->$_POST['certification_id']);
您不访问请求数据。
而不是这样做 -
$request->certification_id
它将为您提供certification_id请求的数组。
我还希望您阅读文档并首先学习基础知识 -
https://laravel.com/docs/5.7/requests