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

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

如何在 laravel 驗證中驗證陣列數據的唯一復合鍵

如何在 laravel 驗證中驗證陣列數據的唯一復合鍵

PHP
MMTTMM 2022-08-19 09:49:57
請求數據    {        "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_scores成功驗證數據。    {        "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貢獻1798條經驗 獲得超3個贊

在搜索了許多博客,教程,當然還有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-08-19
  • 1 回答
  • 0 關注
  • 115 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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