Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[6.x] Use Model connection if set on Model when validating with exists:Model #31037

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Illuminate/Validation/Concerns/ValidatesAttributes.php
Expand Up @@ -819,6 +819,7 @@ public function parseTable($table)
[$connection, $table] = Str::contains($table, '.') ? explode('.', $table, 2) : [null, $table];

if (Str::contains($table, '\\') && class_exists($table) && is_a($table, Model::class, true)) {
$connection = $connection ?? (new $table)->getConnectionName();
$table = (new $table)->getTable();
}
alexkb marked this conversation as resolved.
Show resolved Hide resolved

Expand Down
12 changes: 12 additions & 0 deletions tests/Validation/ValidationValidatorTest.php
Expand Up @@ -4323,6 +4323,10 @@ public function testParsingTablesFromModels()
$this->assertEquals('connection', $explicit_connection[0]);
$this->assertEquals('explicits', $explicit_connection[1]);

$explicit_model_implicit_connection = $v->parseTable(ExplicitTableAndConnectionModel::class);
$this->assertEquals('connection', $explicit_model_implicit_connection[0]);
$this->assertEquals('explicits', $explicit_model_implicit_connection[1]);

$noneloquent_connection = $v->parseTable('connection.'.NonEloquentModel::class);
$this->assertEquals('connection', $noneloquent_connection[0]);
$this->assertEquals(NonEloquentModel::class, $noneloquent_connection[1]);
Expand Down Expand Up @@ -5198,6 +5202,14 @@ class ExplicitTableModel extends Model
public $timestamps = false;
}

class ExplicitTableAndConnectionModel extends Model
{
protected $table = 'explicits';
protected $connection = 'connection';
protected $guarded = [];
public $timestamps = false;
}

class NonEloquentModel
{
}