php - 从多个模型的表单获取数据的问题 - Laravel
问题描述
有人可以帮助我吗?
我正在尝试从表单中获取数据并划分为几个模型。我从表单获取数组并将该数据添加到每个模型的数据库中有问题。
代码:
<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 App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
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 请求数组。
我还希望您先阅读文档并学习基础知识-
推荐阅读
- javascript - 如何同时输出仪表图和折线图实时数据,问题在server.php页面?
- java - 使用 FirebaseUI 在 recyclerview 内的按钮上实现 onclicklistener
- sorting - 如何按子字段对对象数组进行排序?
- c# - 如何在切换按钮 C# 上建立依赖关系
- spring-boot - 带有 cron 表达式的 Spring Scheduler 停止工作
- java-record - 在 Java 16 项目中声明时,如何解决错误“-source 11 中不支持记录(使用 -source 16 或更高版本启用记录)”?
- wordpress - 页面编辑器应该使用我的主题的排版吗?
- sql - 我能够编译代码,但它给了我一个 sql 命令错误。我怎样才能解决这个问题?
- javascript - 我在开发谷歌插件时,前后端交互出现错误
- java - 我们可以在spring boot中为多部分文件上传设置自定义边界吗