diff --git a/src/Illuminate/Validation/Rules/DatabaseRule.php b/src/Illuminate/Validation/Rules/DatabaseRule.php index ab5e13d6468d..e9b110ba917f 100644 --- a/src/Illuminate/Validation/Rules/DatabaseRule.php +++ b/src/Illuminate/Validation/Rules/DatabaseRule.php @@ -62,11 +62,11 @@ public function resolveTableName($table) return $table; } - $model = new $table; + if (is_subclass_of($table, Model::class)) { + return (new $table)->getTable(); + } - return $model instanceof Model - ? $model->getTable() - : $table; + return $table; } /** diff --git a/tests/Validation/ValidationExistsRuleTest.php b/tests/Validation/ValidationExistsRuleTest.php index 3208b25156e8..d5cce447cfd0 100644 --- a/tests/Validation/ValidationExistsRuleTest.php +++ b/tests/Validation/ValidationExistsRuleTest.php @@ -58,6 +58,10 @@ public function testItCorrectlyFormatsAStringVersionOfTheRule() $rule = new Exists(NoTableNameModel::class, 'column'); $rule->where('foo', 'bar'); $this->assertSame('exists:no_table_name_models,column,foo,"bar"', (string) $rule); + + $rule = new Exists(ClassWithRequiredConstructorParameters::class, 'column'); + $rule->where('foo', 'bar'); + $this->assertSame('exists:'.ClassWithRequiredConstructorParameters::class.',column,foo,"bar"', (string) $rule); } public function testItChoosesValidRecordsUsingWhereInRule() @@ -203,3 +207,15 @@ class NoTableNameModel extends Eloquent protected $guarded = []; public $timestamps = false; } + +class ClassWithRequiredConstructorParameters +{ + private $bar; + private $baz; + + public function __construct($bar, $baz) + { + $this->bar = $bar; + $this->baz = $baz; + } +} diff --git a/tests/Validation/ValidationUniqueRuleTest.php b/tests/Validation/ValidationUniqueRuleTest.php index 46b591e4abce..c967ab7c077c 100644 --- a/tests/Validation/ValidationUniqueRuleTest.php +++ b/tests/Validation/ValidationUniqueRuleTest.php @@ -26,6 +26,10 @@ public function testItCorrectlyFormatsAStringVersionOfTheRule() $rule->where('foo', 'bar'); $this->assertSame('unique:no_table_names,NULL,NULL,id,foo,"bar"', (string) $rule); + $rule = new Unique(ClassWithNonEmptyConstructor::class); + $rule->where('foo', 'bar'); + $this->assertSame('unique:'.ClassWithNonEmptyConstructor::class.',NULL,NULL,id,foo,"bar"', (string) $rule); + $rule = new Unique('table', 'column'); $rule->ignore('Taylor, Otwell', 'id_column'); $rule->where('foo', 'bar'); @@ -78,3 +82,15 @@ class NoTableName extends Model protected $guarded = []; public $timestamps = false; } + +class ClassWithNonEmptyConstructor +{ + private $bar; + private $baz; + + public function __construct($bar, $baz) + { + $this->bar = $bar; + $this->baz = $baz; + } +}