Skip to content

Commit 85176c7

Browse files
authored
Fix query for translatable slug
It was an error with `$additionalQuery` for translatable slug e.g. example it was before fix: ```sql select * from `posts` where json_unquote(json_extract(`slug`, '$."nl"')) = '/' or json_unquote(json_extract(`slug`, '$."en"')) = '/' and `team_id` = 3 ``` it was `WHERE statement1 or statement2 and additionalStatement3` instead of `WHERE (statement1 or statement2) and additionalStatement3`
1 parent 65fe98e commit 85176c7

File tree

1 file changed

+2
-4
lines changed

1 file changed

+2
-4
lines changed

src/HasSlug.php

+2-4
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ protected function generateSubstring($slugSourceString)
190190
return substr($slugSourceString, 0, $this->slugOptions->maximumLength);
191191
}
192192

193-
public static function findBySlug(string $slug, array $columns = ['*'], callable $additionalQuery = null)
193+
public static function findBySlug(string $slug, array $columns = ['*'], ?callable $additionalQuery = null)
194194
{
195195
$modelInstance = new static();
196196
$field = $modelInstance->getSlugOptions()->slugField;
@@ -204,9 +204,7 @@ public static function findBySlug(string $slug, array $columns = ['*'], callable
204204
$currentField = "{$field}->{$currentLocale}";
205205
$fallbackField = "{$field}->{$fallbackLocale}";
206206

207-
$query->where($currentField, $slug);
208-
209-
$query->orWhere($fallbackField, $slug);
207+
$query->where(fn ($query) => $query->where($currentField, $slug)->orWhere($fallbackField, $slug));
210208
} else {
211209
$query->where($field, $slug);
212210
}

0 commit comments

Comments
 (0)