How to change database in validator in Laravel

I want to check the requested values ​​from a controller action this way:

// validate the info, create rules for the inputs $rules = array( 'username' => 'required|min:5|unique:users,username', 'email' => 'required|email|unique:users,email', 'password' => 'required|min:8', ); // run the validation rules on the inputs from the form $validator = Validator::make(Input::all(), $rules); 

Everything is fine if I use the default database, but for this check I need to check the tables in another database. In config, I have two databases:

  'connections' => array( 'main' => array( 'driver' => 'mysql', 'host' => '*******', 'database' => '*******', 'username' => '*******', 'password' => '*******', 'charset' => 'utf8', 'collation' => 'utf8_general_ci', 'prefix' => 'ko_', ), 'server_auth' => array( 'driver' => 'mysql', 'host' => '*******', 'database' => '*******', 'username' => '*******', 'password' => '*******', 'charset' => 'utf8', 'collation' => 'utf8_general_ci', 'prefix' => '', ), ), 

When I call Validation, it checks the "unique" rule in my database by default, so I need to change it, but I can not find anything to do it.

+5
source share
1 answer

I did it as follows:

  $verifier = App::make('validation.presence'); $verifier->setConnection('server_auth'); // validate the info, create rules for the inputs $rules = User::$rules = array( 'username' => 'required|min:5|unique:users,username', 'email' => 'required|email|unique:users,email', 'password' => 'required|min:8', ); // run the validation rules on the inputs from the form $validator = Validator::make(Input::all(), $rules); $validator->setPresenceVerifier($verifier); 

A solution for others who have had this problem.

+9
source

Source: https://habr.com/ru/post/1208521/


All Articles