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

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

如何在 Laravel 的請求類中使用 if/else

如何在 Laravel 的請求類中使用 if/else

PHP
互換的青春 2023-09-22 16:51:51
我只使用一個頁面來“創建廣告頁面”。如何對查詢('type')使用多個驗證規則?廣告刀片.php@if ($request->query('type') == 'text')   //fields@elseif($request->query('type') == 'post')   //fields@elseif($request->query('type') == 'banner')   //fields@endif廣告請求.phppublic function rules(Request $request){    //this if not work. how to resolve?    if ($request->query('type') == 'text'){        return [            'title'  => ['required','max:255', Rule::unique('ads')->ignore($this->route('ad'))],            'url' => 'required',            'type' => 'required|in:text,banner,post,sidebar',            'status' => 'nullable',            'btn' => 'nullable',            'pic' => 'nullable',            'description' => 'required',        ];    }}如何對查詢('type')使用多個驗證規則?
查看完整描述

1 回答

?
慕的地6264312

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

在請求類中,您可以通過方法訪問接收到的數據all()。


$data = $this->all();

$type = isset($data['type'])? $data['type'] : null;


switch($type) {

     case "text": 

        return [

           'url' => 'required'

        ]

}

請注意,該rules()方法返回的是一個普通的 PHP 數組,它可以是不同數組的合并。您可能有一些與此type字段無關的規則。


$commonRules = [

   'title' => 'required',

   'email' => 'required|email',

];

$specificRules = [];


$data = $this->all();

$type = isset($data['type'])? $data['type'] : null;


switch($type) {

     case "text": 

        $specificRules = [

           'url' => 'required',

        ];

        break;


     case "foo":

        $specificRules = [

            'btn' => 'nullable',

        ];

        break;

}


return array_merge($commonRules, $specificRules);


查看完整回答
反對 回復 2023-09-22
  • 1 回答
  • 0 關注
  • 97 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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