My application has two database connection:
mysql_default_connection => default_database
mysql_custom_connection => custom_database
And i'm having some problems with Laravel unique validation.
protected $connection = 'mysql_custom_connection';
protected $table = 'aluno';
public $rules = [
'email' => 'required|email|unique:custom_database.aluno'
];
When i try to save a new model, the unique rules works but When retrive some model from aluno and try to save it like:
$aluno = Aluno::find(1);
$aluno->attribute = 'new value';
$aluno->save();
This log:
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'default_database.aluno' doesn't exist (SQL: select count(*) as aggregate from `aluno` where `email` = [email protected] and `id` <> 50)
If i remove the unique rules from validation, work fine.
Particularly, i can't understand why querying in Table 'default_database.aluno' because this database isn't the model connection as above.
Can someone explain to me why this occurs?
My connection config
'connections' => [
'mysql_default_connection' => [
'driver' => 'mysql',
'host' => 'localhost',
'port' => 3306,
'database' => 'default_database',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
],
'mysql_custom_connection' => [
'driver' => 'mysql',
'host' => 'localhost',
'port' => 3306,
'database' => 'custom_database',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
],
];
'default' => 'mysql_default_connection',
Relevant part from aluno model
protected $connection = 'mysql_custom_connection';
protected $table = 'aluno';
public $rules = [
'nome' => 'required|between:5, 45',
'email' => 'required|email|unique:custom_database.aluno|max:64'
];
This rules works on create new models
SOLUTION
I'm using OctoberCMS that implement Laravel and this issue is a OctoberCMS trouble and not Laravel, sorry. To make validations on the model, we need to use the October Validation trait that get the $rules, $attributeNames and $customMessages arrays to call Validator::make()....
In this trait, October simply ignores the model connection on validation
/**
* Instantiates the validator used by the validation process, depending if the class is being used inside or
* outside of Laravel.
* @return \Illuminate\Validation\Validator
*/
protected static function makeValidator($data, $rules, $customMessages, $attributeNames)
{
return Validator::make($data, $rules, $customMessages, $attributeNames);
}
And
/*
* Hand over to the validator
*/
$validator = self::makeValidator($data, $rules, $customMessages, $attributeNames);
So, i do needed changes
protected static function makeValidator($data, $rules, $customMessages, $attributeNames,$connection = null)
{
$validator = Validator::make($data, $rules, $customMessages, $attributeNames);
if($connection !== null) {
$validator->getPresenceVerifier()->setConnection($connection);
}
return $validator;
}
And
$validator = self::makeValidator($data, $rules, $customMessages, $attributeNames,$this->connection);
Now works like a charm
$rules = [
'email' => 'required|unique:aluno'
];