Skip to content

Commit

Permalink
[8.x] Fix inconsistency in table names in validator (#37606)
Browse files Browse the repository at this point in the history
* fix inconsistency in table names in validator

* Update ValidatesAttributes.php

Co-authored-by: Taylor Otwell <taylor@laravel.com>
  • Loading branch information
mvalim and taylorotwell authored Jun 5, 2021
1 parent 6c83713 commit ceb3217
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/Illuminate/Validation/Concerns/ValidatesAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -805,6 +805,11 @@ public function parseTable($table)

$table = $model->getTable();
$connection = $connection ?? $model->getConnectionName();

if (Str::contains($table, '.') && Str::startsWith($table, $connection)) {
$connection = null;
}

$idColumn = $model->getKeyName();
}

Expand Down
15 changes: 15 additions & 0 deletions tests/Validation/ValidationValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4955,6 +4955,14 @@ public function testParsingTablesFromModels()
$this->assertNull($explicit_no_connection[0]);
$this->assertSame('explicits', $explicit_no_connection[1]);

$explicit_model_with_prefix = $v->parseTable(ExplicitPrefixedTableModel::class);
$this->assertNull($explicit_model_with_prefix[0]);
$this->assertSame('prefix.explicits', $explicit_model_with_prefix[1]);

$explicit_table_with_connection_prefix = $v->parseTable('connection.table');
$this->assertSame('connection', $explicit_table_with_connection_prefix[0]);
$this->assertSame('table', $explicit_table_with_connection_prefix[1]);

$noneloquent_no_connection = $v->parseTable(NonEloquentModel::class);
$this->assertNull($noneloquent_no_connection[0]);
$this->assertEquals(NonEloquentModel::class, $noneloquent_no_connection[1]);
Expand Down Expand Up @@ -6208,6 +6216,13 @@ class ExplicitTableModel extends Model
public $timestamps = false;
}

class ExplicitPrefixedTableModel extends Model
{
protected $table = 'prefix.explicits';
protected $guarded = [];
public $timestamps = false;
}

class ExplicitTableAndConnectionModel extends Model
{
protected $table = 'explicits';
Expand Down

0 comments on commit ceb3217

Please sign in to comment.