php - Laravel validation causing query exception on save models -
my application has 2 database connection:
mysql_default_connection => default_database mysql_custom_connection => custom_database
and i'm having problems laravel unique
validation.
protected $connection = 'mysql_custom_connection'; protected $table = 'aluno'; public $rules = [ 'email' => 'required|email|unique:custom_database.aluno' ];
when try save new model, unique rules works when retrive model aluno
, try save 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(*) aggregate `aluno` `email` = aluno3@email.com , `id` <> 50)
if remove unique
rules validation, work fine.
particularly, can't understand why querying in table 'default_database.aluno'
because database isn't model connection above.
can explain me why 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 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 implement laravel , issue octobercms trouble , not laravel, sorry. make validations on model, need use october validation trait $rules, $attributenames , $custommessages
arrays call validator::make()...
.
in trait, october ignores model connection on validation
/** * instantiates validator used validation process, depending if class 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 on validator */ $validator = self::makevalidator($data, $rules, $custommessages, $attributenames);
so, 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 charm
$rules = [ 'email' => 'required|unique:aluno' ];
just remove default_database validation, try
public $rules = [ 'email' => 'required|email|unique:aluno' ];
or try if want specify custom connection
public $rules = [ 'email' => 'required|email|unique:mysql_custom_connection.aluno' ];
Comments
Post a Comment