From 8158354b48adb97d2f3b0e96a026a15c5a2bf649 Mon Sep 17 00:00:00 2001 From: Alex Bergin Date: Mon, 6 Jan 2020 16:16:47 +0800 Subject: [PATCH 1/2] Use Model connection if set on Model when validating with exists:Model --- .../Validation/Concerns/ValidatesAttributes.php | 1 + tests/Validation/ValidationValidatorTest.php | 12 ++++++++++++ 2 files changed, 13 insertions(+) diff --git a/src/Illuminate/Validation/Concerns/ValidatesAttributes.php b/src/Illuminate/Validation/Concerns/ValidatesAttributes.php index 255b797927e7..dda38ba9a11c 100644 --- a/src/Illuminate/Validation/Concerns/ValidatesAttributes.php +++ b/src/Illuminate/Validation/Concerns/ValidatesAttributes.php @@ -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(); } diff --git a/tests/Validation/ValidationValidatorTest.php b/tests/Validation/ValidationValidatorTest.php index 1457857560f5..1b1864511e29 100755 --- a/tests/Validation/ValidationValidatorTest.php +++ b/tests/Validation/ValidationValidatorTest.php @@ -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]); @@ -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 { } From 69542b9414b0a43e18802343e8cad0ccefc6cca7 Mon Sep 17 00:00:00 2001 From: Alex Bergin Date: Mon, 6 Jan 2020 20:52:46 +0800 Subject: [PATCH 2/2] create model instance to avoid multiple instances of model --- src/Illuminate/Validation/Concerns/ValidatesAttributes.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Illuminate/Validation/Concerns/ValidatesAttributes.php b/src/Illuminate/Validation/Concerns/ValidatesAttributes.php index dda38ba9a11c..397b832834fc 100644 --- a/src/Illuminate/Validation/Concerns/ValidatesAttributes.php +++ b/src/Illuminate/Validation/Concerns/ValidatesAttributes.php @@ -819,8 +819,9 @@ 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(); + $model = new $table; + $table = $model->getTable(); + $connection = $connection ?? $model->getConnectionName(); } return [$connection, $table];