1
votes

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'
];
2
Please show config. First check if you provided custom connection corectly in config. Try $aluno = new Aluno; $aluno->setConnection('custom'); $aluno->find(1); - Eimantas Gabrielius
@EimantasGabrielius but my connection is already setted on the model attribute $connection and work fine, the problem is when i use unique rule - Alexandre Thebaldi
Please show how you're doing the validation. Its trough model or requests ? - Eimantas Gabrielius
@EimantasGabrielius model - Alexandre Thebaldi
i've posted an answer - Eimantas Gabrielius

2 Answers

0
votes

just remove default_database from your validation, try this

public $rules = [
    'email' => 'required|email|unique:aluno'
];

or try this if you want to specify the custom connection

public $rules = [
    'email' => 'required|email|unique:mysql_custom_connection.aluno'
];
0
votes

Try this

     $rules = [
          'email' => 'unique:mysql_custom_connection.aluno,email'
     ];

It works, i've tried now. Just understand that aluno is table, you need to check unique FIELD not table.