Как передать значение в Rulee :: exist () - PullRequest
0 голосов
/ 03 ноября 2018

У меня есть следующий код:

$postalcode=$data['postal_code'];   
 return Validator::make($data, [
                'name' => ['required','string','max:255'],
                'email' => ['required','string','email','max:255','unique:users'],
                'password' => ['required','string','min:8','confirmed'],
                'postal_code' =>['required','string','max:255',Rule::exists('zipcodes')->where(function ($query) {
                    $query->whereRaw('REPLACE(POSTAL_CODE,\' \',\'\')= ?',[str_replace(' ','',$postalcode)]);
                })],
            ]);

но выдает следующую ошибку:

«Неопределенная переменная: почтовый индекс»

так что я хочу передать переменную $ postalcode в правило, но я не знаю как?

Ответы [ 2 ]

0 голосов
/ 03 ноября 2018
Try with this, pass paramiter using laravel eager loading (function ($query) use ($postalcode)).Now Don't give Undefined variable: postalcode.

$postalcode=$data['postal_code'];   
 return Validator::make($data, [
                    'name' => ['required','string','max:255'],
                    'email' => ['required','string','email','max:255','unique:users'],
                    'password' => ['required','string','min:8','confirmed'],
                    'postal_code' =>['required','string','max:255',Rule::exists('zipcodes')->where(function ($query) use ($postalcode){
                        $query->whereRaw('REPLACE(POSTAL_CODE)= ?',[str_replace(' ','',$postalcode)]);
                    })],
                ]);
0 голосов
/ 03 ноября 2018
Rule::exists('zipcodes')->where(function ($query) use($postalcode){
                    $query->whereRaw('REPLACE(POSTAL_CODE,\' \',\'\')= ?',[str_replace(' ','',$postalcode)]);
                });

Вы забыли написать use($postalcode);

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...