亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在 laravel Validations 中驗證數組數據的唯一復合鍵

如何在 laravel Validations 中驗證數組數據的唯一復合鍵

PHP
慕斯709654 2022-07-29 15:29:30
請求數據    {        "exam_id": 10,        "exam_scores": [        {          "student_id": 1,          "subject_id": 1,          "marks": 50,        },        {          "student_id": 1,          "subject_id": 2,          "marks": 70,        },        {          "student_id": 1,          "subject_id": 3,          "marks": 80,        }        ],    }其中 student_id 和 subject_id 是唯一的復合鍵,如何使用接受數據數組的規則方法為復合鍵制作驗證器()我試過但它沒有按預期工作。$validator = Validator::make(request()->all(), [    "exam_id"=> "required|integer",    "exam_scores"=> "required|array",    'exam_scores.*.student_id' => [        Rule::unique('results')->where(function ($query) {            return $query                ->whereStudent_idAndSubject_id(request()->get('exam_scores.*.student_id'),request()->get('exam_scores.*.subject_id'))        })    ],]);以下請求不應驗證數據。但它驗證成功。{        "exam_id": 10,        "exam_scores": [        {          "student_id": 1,          "subject_id": 1,          "marks": 50,        },        {          "student_id": 1,          "subject_id": 1,          "marks": 70,        }       ],    }下面的請求成功地驗證了具有預期的單個考試分數對象的數據。    {        "exam_id": 10,        "exam_scores": {            "student_id": 1,            "subject_id": 1,            "marks": 50,        }    }$validator = Validator::make(request()->all(), [    "exam_id"=> "required|integer",    "exam_scores"=> "required|array",    'exam_scores.student_id' => [    Rule::unique('results')->where(function ($query) {        return $query            ->whereStudent_idAndSubject_id(request()->get('exam_scores.student_id'),request()->get('exam_scores.subject_id'))    })],]);
查看完整描述

1 回答

?
躍然一笑

TA貢獻1826條經驗 獲得超6個贊

在搜索了很多博客、教程和當然 laravel 文檔之后,我得到了解決我的問題的東西,這里是博客鏈接。這不是我真正想要的,但它明確了我必須做什么的概念。這個人拯救了我的一天。

驗證動態請求值

這是一個例子。

namespace App\Http\Requests;    

use App\Http\Requests\Request;


class OrderRequest extends Request

{


    /**

     * 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()

    {

        $rules = [

            'name' => 'required|max:255',

        ];


        foreach ($this->request->get('items') as $key => $val) {

            $rules['items.' . $key] = 'required|max:10';

        }


        return $rules;

    }


    public function messages()

    {

        $messages = [];

        foreach ($this->request->get('items') as $key => $val) {

            $messages['items.' . $key . '.max'] = 'The field labeled "Book Title ' . $key . '" must be less than :max characters.';

        }

        return $messages;

    }


}

解決方案非常簡單易行。


查看完整回答
反對 回復 2022-07-29
  • 1 回答
  • 0 關注
  • 200 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號