Skip to content

Commit

Permalink
Fix slug issue
Browse files Browse the repository at this point in the history
  • Loading branch information
Tofandel committed Jun 18, 2024
1 parent f1041d1 commit 6d5c1ba
Showing 1 changed file with 10 additions and 13 deletions.
23 changes: 10 additions & 13 deletions src/Models/Behaviors/HasSlug.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use A17\Twill\Models\Model;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;

trait HasSlug
Expand Down Expand Up @@ -109,13 +110,7 @@ public function restoreSlugs(): void
*/
public function handleSlugsOnSave(): void
{
if ($this->twillSlugData === []) {
return;
}

foreach (getLocales() as $locale) {
$this->disableLocaleSlugs($locale);
}
$this->disableLocaleSlugs();

$slugParams = $this->twillSlugData !== [] ? $this->twillSlugData : $this->getSlugParams();

Expand Down Expand Up @@ -208,12 +203,14 @@ protected function addOneSlug(array $slugParams): void
$this->disableLocaleSlugs($slugParams['locale'], $slugModel->getKey());
}

public function disableLocaleSlugs(string $locale, int $except_slug_id = 0): void
public function disableLocaleSlugs(string|array $locale = null, int $except_slug_id = 0): void
{
$this->getSlugModelClass()::where($this->getForeignKey(), $this->id)
->where('id', '<>', $except_slug_id)
->where('locale', $locale)
->update(['active' => 0]);
$query = $this->getSlugModelClass()::where($this->getForeignKey(), $this->id)
->where('id', '<>', $except_slug_id);
if ($locale !== null) {
$query->whereIn('locale', Arr::wrap($locale));
}
$query->update(['active' => 0]);
}

private function suffixSlugIfExisting(array $slugParams): string
Expand Down Expand Up @@ -345,7 +342,7 @@ public function getSlugParams(?string $locale = null): ?array
}

$slugParam = [
'active' => $translation->active,
'active' => $translation->active ?? true,
'slug' => $translation->$slugAttribute ?? $this->$slugAttribute,
'locale' => $translation->locale,
] + $slugDependenciesAttributes;
Expand Down

0 comments on commit 6d5c1ba

Please sign in to comment.