Skip to content

Commit a8a12e5

Browse files
bkintanarivanvermeyen
authored andcommitted
Allow adding a database connection to the rule
Allow adding a database connection to the rule using a dot notation: `unique_translation:db2.users,name`
1 parent 3b0f7fc commit a8a12e5

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

src/UniqueTranslationValidator.php

+15-10
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,17 @@ public function validate($attribute, $value, $parameters, $validator) {
2020
$attributeParts = explode('.', $attribute);
2121
$name = $attributeParts[0];
2222
$locale = $attributeParts[1] ?? app()->getLocale();
23-
$table = $parameters[0] ?? null;
2423
$column = $this->filterNullValues($parameters[1] ?? null) ?: $name;
2524
$ignoreValue = $this->filterNullValues($parameters[2] ?? null);
2625
$ignoreColumn = $this->filterNullValues($parameters[3] ?? null);
2726

28-
$isUnique = $this->isUnique($value, $locale, $table, $column, $ignoreValue, $ignoreColumn);
27+
// dd($parameters);
28+
$table = $parameters[0] ?? null;
29+
$tableParts = explode('.', $table);
30+
$connection = isset($tableParts[1]) ? $tableParts[0] : config('database.default');
31+
$table = $tableParts[1] ?? $tableParts[0];
32+
33+
$isUnique = $this->isUnique($value, $locale, $connection, $table, $column, $ignoreValue, $ignoreColumn);
2934

3035
if ( ! $isUnique) {
3136
$this->addErrorsToValidator($validator, $parameters, $name, $locale);
@@ -37,9 +42,9 @@ public function validate($attribute, $value, $parameters, $validator) {
3742
/**
3843
* Filter NULL values.
3944
*
40-
* @param mixed $value
45+
* @param string|null $value
4146
*
42-
* @return mixed
47+
* @return string|null
4348
*/
4449
protected function filterNullValues($value)
4550
{
@@ -55,7 +60,7 @@ protected function filterNullValues($value)
5560
/**
5661
* Check if a translation is unique.
5762
*
58-
* @param string $value
63+
* @param mixed $value
5964
* @param string $locale
6065
* @param string $table
6166
* @param string $column
@@ -64,9 +69,9 @@ protected function filterNullValues($value)
6469
*
6570
* @return bool
6671
*/
67-
protected function isUnique($value, $locale, $table, $column, $ignoreValue = null, $ignoreColumn = null)
72+
protected function isUnique($value, $locale, $connection, $table, $column, $ignoreValue = null, $ignoreColumn = null)
6873
{
69-
$query = $this->findTranslation($table, $column, $locale, $value);
74+
$query = $this->findTranslation($connection, $table, $column, $locale, $value);
7075
$query = $this->ignore($query, $ignoreColumn, $ignoreValue);
7176

7277
$isUnique = $query->count() === 0;
@@ -80,13 +85,13 @@ protected function isUnique($value, $locale, $table, $column, $ignoreValue = nul
8085
* @param string $table
8186
* @param string $column
8287
* @param string $locale
83-
* @param string $value
88+
* @param mixed $value
8489
*
8590
* @return \Illuminate\Database\Query\Builder
8691
*/
87-
protected function findTranslation($table, $column, $locale, $value)
92+
protected function findTranslation($connection, $table, $column, $locale, $value)
8893
{
89-
return DB::table($table)->where("{$column}->{$locale}", '=', $value);
94+
return DB::connection($connection)->table($table)->where("{$column}->{$locale}", '=', $value);
9095
}
9196

9297
/**

0 commit comments

Comments
 (0)