第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

如何在 laravel 驗證中驗證陣列數(shù)據(jù)的唯一復(fù)合鍵

如何在 laravel 驗證中驗證陣列數(shù)據(jù)的唯一復(fù)合鍵

PHP
MMTTMM 2022-08-19 09:49:57
請求數(shù)據(jù)    {        "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是唯一的復(fù)合鍵,如何使用規(guī)則方法為復(fù)合鍵制作驗證程序,該方法接受我嘗試過的數(shù)據(jù)數(shù)組(),但它無法按預(yù)期工作。$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'))        })    ],]);以下請求不應(yīng)驗證數(shù)據(jù)。但它驗證成功。{        "exam_id": 10,        "exam_scores": [        {          "student_id": 1,          "subject_id": 1,          "marks": 50,        },        {          "student_id": 1,          "subject_id": 1,          "marks": 70,        }       ],    }以下請求使用預(yù)期的單個對象exam_scores成功驗證數(shù)據(jù)。    {        "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貢獻(xiàn)1798條經(jīng)驗 獲得超3個贊

在搜索了許多博客,教程,當(dāng)然還有l(wèi)aravel文檔之后,我得到了一些解決我的問題是博客鏈接的東西。這不是我真正想要的,但它清楚地說明了我必須做什么的概念。這個家伙拯救了我的一天。

驗證動態(tài)請求值

下面是一個示例。

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;

    }


}

解決方案非常簡單易行。


查看完整回答
反對 回復(fù) 2022-08-19
  • 1 回答
  • 0 關(guān)注
  • 105 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號