Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/258.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 求解具有关系的字段的laravel验证_Php_Laravel_Laravel Blade - Fatal编程技术网

Php 求解具有关系的字段的laravel验证

Php 求解具有关系的字段的laravel验证,php,laravel,laravel-blade,Php,Laravel,Laravel Blade,我有一些问题,我的刀片模板在相关表上的下拉选择验证失败。我试图寻找答案,但找不到 在validate函数中临时注释掉数组中的“business\u unit\u id”字段以避免错误 在控制器更新功能中: $item = CostCenter::findOrFail($id); $validatedData = $request->validate([ 'code' => 'bail|min:4|required|max:6', 'de

我有一些问题,我的刀片模板在相关表上的下拉选择验证失败。我试图寻找答案,但找不到

在validate函数中临时注释掉数组中的“business\u unit\u id”字段以避免错误

在控制器更新功能中:

    $item = CostCenter::findOrFail($id);
    $validatedData = $request->validate([
        'code' => 'bail|min:4|required|max:6',
        'descr' => 'bail|min:4|required|max:50',
        'business_unit_id' => 'required|integer',
    ]);

    $item->fill($validatedData);
    $item->business_unit_id = $request->businessUnit;

    $item->save();
    $request->session()->flash('status', 'Cost centers record was updated!');

    return redirect()->route('costCenters.show', ['cost_centers' => $item->id]);
在刀片模板中:

        <label for="businessUnit" class="col-sm-2 col-form-label">Business Unit</label>
            <select name="businessUnit" class="form-control">
                @foreach (App\Models\BusinessUnit::all() as $data)
                    <option value="{{ $data->id }}"
                        {{ old('business_unit_id', $cost_centers->business_unit_id ?? '1')
                            != $data->id ?: 'selected' }}>
                        {{ $data->code }} - {{ $data->descr }}
                    </option>
                @endforeach
            </select>
业务单元
@foreach(App\Models\BusinessUnit::all()作为$data)
业务单位识别号1')
!= $数据->id?:“选定”}>
{{$data->code}-{{$data->descr}
@endforeach

收到的通知是“需要业务部门id字段”。使用我的错误帮助程序。

对于controller中的任何请求数据验证,您可以这样尝试:

$rule=array(
        'code' => 'bail|min:4|required|max:6',
        'descr' => 'bail|min:4|required|max:50',
        'business_unit_id' => 'required|integer',
);
$validator= Validator::make($request->all(),$rule);

对于控制器中的任何请求数据验证,您可以这样尝试:

$rule=array(
        'code' => 'bail|min:4|required|max:6',
        'descr' => 'bail|min:4|required|max:50',
        'business_unit_id' => 'required|integer',
);
$validator= Validator::make($request->all(),$rule);
使用businessUnit而不是business\u unit\u id 因为我们必须使用选择标记的名称。。。 不要使用选项标记的id

使用businessUnit而不是business\u unit\u id 因为我们必须使用选择标记的名称。。。
不要使用选项标签的id。

您可以尝试此代码…并使用关键字来定义代码的类型,如使用验证程序

    $rules = [
           'code' => 'bail|min:4|required|max:6',
           'descr' => 'bail|min:4|required|max:50',
           'businessUnit' => 'required|integer|exists:business_unit,id',
    ];

    $message = [
        'code.required' => 'code is required.',
        'descr.required' => 'description field is required.',
        'businessUnit.required' => 'The business unit id field is required.',

    ];

     $validator = Validator::make($request->all(), $rules,$message);
      if ($validator->fails()) {
      $arr = array("status"=>400,"msg"=>$validator->errors()- 
     >first(),"result"=>array());
      }
       else
       {
      your code write in this section
        }

您可以尝试此代码…并使用关键字来定义代码的类型,如使用验证程序

    $rules = [
           'code' => 'bail|min:4|required|max:6',
           'descr' => 'bail|min:4|required|max:50',
           'businessUnit' => 'required|integer|exists:business_unit,id',
    ];

    $message = [
        'code.required' => 'code is required.',
        'descr.required' => 'description field is required.',
        'businessUnit.required' => 'The business unit id field is required.',

    ];

     $validator = Validator::make($request->all(), $rules,$message);
      if ($validator->fails()) {
      $arr = array("status"=>400,"msg"=>$validator->errors()- 
     >first(),"result"=>array());
      }
       else
       {
      your code write in this section
        }

您已将select元素命名为
“businessUnit”
。它应该命名为
“business\u unit\u id”
。我以前尝试将控件命名为需要验证的模型字段,但验证不起作用。它呈现SQLSTATE[23000]的错误:[Microsoft][ODBC Driver 13 for SQL Server][SQL Server]无法将值NULL插入“业务单位id”列中的表“epms.dbo.cost_centers”;列不允许空值。更新失败。(SQL:update[cost_centers]set[business_unit_id]=?,[cost_centers]。[updated_at]=2019-08-14 09:25:11.360其中[id]=1)你好,我想让你知道你的建议现在起作用了。我不明白为什么在我之前尝试这些时它不起作用。我现在意识到,它以前可能不起作用的原因是我在控制器中的“创建”而不是“更新”中提交了更改。谢谢。您已将select元素命名为
“businessUnit”
。它应该命名为
“business\u unit\u id”
。我以前尝试将控件命名为需要验证的模型字段,但验证不起作用。它呈现SQLSTATE[23000]的错误:[Microsoft][ODBC Driver 13 for SQL Server][SQL Server]无法将值NULL插入“业务单位id”列中的表“epms.dbo.cost_centers”;列不允许空值。更新失败。(SQL:update[cost_centers]set[business_unit_id]=?,[cost_centers]。[updated_at]=2019-08-14 09:25:11.360其中[id]=1)你好,我想让你知道你的建议现在起作用了。我不明白为什么在我之前尝试这些时它不起作用。我现在意识到,它以前可能不起作用的原因是我在控制器中的“创建”而不是“更新”中提交了更改。谢谢。我以前也试过,但失败了,现在又试了一次,结果还是一样。SQLSTATE[42S22]:[Microsoft][ODBC驱动程序13 for SQL Server][SQL Server]列名“businessUnit”无效。(SQL:update[cost_centers]set[businessUnit]=89[cost_centers].[updated_at]=2019-08-14 09:47:01.178其中[id]=1)。我认为上面的注释更合适,因为select名称与正在验证的字段相同。我只是不知道为什么它没有通过验证,即使已经确认传递给控制器的请求字段有值。在验证数据之前,只需检查刀片服务器返回的所有数据……使用dd($request->all());我以前也试过,但失败了,现在又试了一次,结果还是一样。SQLSTATE[42S22]:[Microsoft][ODBC驱动程序13 for SQL Server][SQL Server]列名“businessUnit”无效。(SQL:update[cost_centers]set[businessUnit]=89[cost_centers].[updated_at]=2019-08-14 09:47:01.178其中[id]=1)。我认为上面的注释更合适,因为select名称与正在验证的字段相同。我只是不知道为什么它没有通过验证,即使已经确认传递给控制器的请求字段有值。在验证数据之前,只需检查刀片服务器返回的所有数据……使用dd($request->all());