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

[8.x] Add missing fix to DatabaseRule::resolveTableName fix #37580 #37621

Merged
merged 2 commits into from
Jun 8, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/Illuminate/Validation/Rules/DatabaseRule.php
Original file line number Diff line number Diff line change
@@ -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()])));
11 changes: 11 additions & 0 deletions tests/Validation/ValidationExistsRuleTest.php
Original file line number Diff line number Diff line change
@@ -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);
@@ -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';
10 changes: 10 additions & 0 deletions tests/Validation/ValidationUniqueRuleTest.php
Original file line number Diff line number Diff line change
@@ -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');
@@ -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 = [];