Reputation: 800
I make Request to Validate requested values from controller action by this way:
namespace App\Http\Requests;
use App\Http\Requests\Request;
class AccountsRequest extends Request {
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize() {
return TRUE;
}
public function rules() {
return [
'email' => 'email|required|max:255|unique:accounts',
'password' => 'min:6|required'
];
}
}
All is fine if I'm using the default database, but for this Validation I need to check the tables in other database. In config I have two databases:
'connections' => [
'sqlite' => [
'driver' => 'sqlite',
'database' => database_path('database.sqlite'),
'prefix' => '',
],
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'sait'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
'mysql2' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'account'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
When I call the Validation, it is checking by "unique" rule in my default database so I need to change it, but I cannot found anywhere how to do this.
Upvotes: 3
Views: 144
Reputation: 1577
Did you see this in the docs for the 'unique' rule?
$verifier = App::make('validation.presence');
$verifier->setConnection('connectionName');
$validator = Validator::make($input, [
'name' => 'required',
'password' => 'required|min:8',
'email' => 'required|email|unique:users',
]);
$validator->setPresenceVerifier($verifier);
You can use the setConnection() method to specify how you want unique to be determined.
Upvotes: 0
Reputation: 1959
According to the docs unique - Custom Database Connection instead of
'email' => 'email|required|max:255|unique:accounts',
you need to do
'email' => 'email|required|max:255|unique:mysql2.accounts',
I guessed its mysql2 as you didnt mention.
Upvotes: 5