从多个模型的表单获取数据的问题 - Laravel

问题描述 投票:0回答:1

有人能帮助我吗?

我正在尝试从表单中获取数据并分成几个模型。我有从表单获取数组的问题,并将该数据添加到每个模型的数据库。

码:

    <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'

        ];
    }
}
php laravel
1个回答
0
投票

这是错误的 -

$certifications = new WaybillCertification($request->$_POST['certification_id']);

您不会像这样访问请求数据。

这样做 - 而不是 -

$request->certification_id

它将为您提供cert_id请求的数组。

我还希望您先阅读文档并学习基础知识 -

https://laravel.com/docs/5.7/requests

© www.soinside.com 2019 - 2024. All rights reserved.