Skip to content

Commit

Permalink
[8.x] Add missing fix to DatabaseRule::resolveTableName fix #37580 (#…
Browse files Browse the repository at this point in the history
…37621)

* add missing fix to DatabaseRule::resolveTableName

* style CI fixes
  • Loading branch information
mvalim authored Jun 8, 2021
1 parent b4cfbd5 commit 5a085e9
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/Illuminate/Validation/Rules/DatabaseRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ public function resolveTableName($table)
if (is_subclass_of($table, Model::class)) {
$model = new $table;

if (Str::contains($model->getTable(), '.')) {
return $table;
}

return implode('.', array_map(function (string $part) {
return trim($part, '.');
}, array_filter([$model->getConnectionName(), $model->getTable()])));
Expand Down
11 changes: 11 additions & 0 deletions tests/Validation/ValidationExistsRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ public function testItCorrectlyFormatsAStringVersionOfTheRule()
$rule->where('foo', 'bar');
$this->assertSame('exists:users,NULL,foo,"bar"', (string) $rule);

$rule = new Exists(UserWithPrefixedTable::class);
$rule->where('foo', 'bar');
$this->assertSame('exists:'.UserWithPrefixedTable::class.',NULL,foo,"bar"', (string) $rule);

$rule = new Exists('table', 'column');
$rule->where('foo', 'bar');
$this->assertSame('exists:table,column,foo,"bar"', (string) $rule);
Expand Down Expand Up @@ -235,6 +239,13 @@ class User extends Eloquent
public $timestamps = false;
}

class UserWithPrefixedTable extends Eloquent
{
protected $table = 'public.users';
protected $guarded = [];
public $timestamps = false;
}

class UserWithConnection extends User
{
protected $connection = 'mysql';
Expand Down
10 changes: 10 additions & 0 deletions tests/Validation/ValidationUniqueRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ public function testItCorrectlyFormatsAStringVersionOfTheRule()
$rule->where('foo', 'bar');
$this->assertSame('unique:table,column,"Taylor, Otwell",id_column,foo,"bar"', (string) $rule);

$rule = new Unique(PrefixedTableEloquentModelStub::class);
$this->assertSame('unique:'.PrefixedTableEloquentModelStub::class.',NULL,NULL,id', (string) $rule);

$rule = new Unique(EloquentModelStub::class, 'column');
$rule->ignore('Taylor, Otwell', 'id_column');
$rule->where('foo', 'bar');
Expand Down Expand Up @@ -87,6 +90,13 @@ class EloquentModelStub extends Model
protected $guarded = [];
}

class PrefixedTableEloquentModelStub extends Model
{
protected $table = 'public.table';
protected $primaryKey = 'id_column';
protected $guarded = [];
}

class NoTableName extends Model
{
protected $guarded = [];
Expand Down

0 comments on commit 5a085e9

Please sign in to comment.