Skip to content

Commit

Permalink
Merge pull request #249 from Vediovis/main
Browse files Browse the repository at this point in the history
Allow set slug suffix starting number
  • Loading branch information
freekmurze authored Dec 7, 2022
2 parents 149ddfb + 8ff11e1 commit 8d42bbf
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 1 deletion.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,20 @@ public function getSlugOptions() : SlugOptions
}
```

### Setting the slug suffix starting index

By default, suffix index starts from 1, you can set starting number.

```php
public function getSlugOptions() : SlugOptions
{
return SlugOptions::create()
->generateSlugsFrom('name')
->saveSlugsTo('slug')
->startSlugSuffixFrom(2);
}
```

### Integration with laravel-translatable

You can use this package along with [laravel-translatable](https://github.com/spatie/laravel-translatable) to generate a slug for each locale. Instead of using the `HasSlug` trait, you must use the `HasTranslatableSlug` trait, and add the name of the slug field to the `$translatable` array. For slugs that are generated from a single field _or_ multiple fields, you don't have to change anything else.
Expand Down
2 changes: 1 addition & 1 deletion src/HasSlug.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ protected function getSlugSourceStringFromCallable(): string
protected function makeSlugUnique(string $slug): string
{
$originalSlug = $slug;
$i = 1;
$i = $this->slugOptions->startSlugSuffixFrom;

while ($this->otherRecordExistsWithSlug($slug) || $slug === '') {
$slug = $originalSlug.$this->slugOptions->slugSeparator.$i++;
Expand Down
9 changes: 9 additions & 0 deletions src/SlugOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ class SlugOptions

public array $translatableLocales = [];

public int $startSlugSuffixFrom = 1;

public static function create(): static
{
return new static();
Expand Down Expand Up @@ -124,4 +126,11 @@ public function extraScope(callable $callbackMethod): self

return $this;
}

public function startSlugSuffixFrom(int $startSlugSuffixFrom): self
{
$this->startSlugSuffixFrom = max(1, $startSlugSuffixFrom);

return $this;
}
}
18 changes: 18 additions & 0 deletions tests/HasSlugTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -315,3 +315,21 @@ public function getSlugOptions(): SlugOptions
expect($model->url)->toEqual('this-is-a-test');
expect($replica->url)->toEqual('this-is-a-test-1');
});

it('can generate slug suffix starting from given number', function () {
$model = new class () extends TestModel {
public function getSlugOptions(): SlugOptions
{
return parent::getSlugOptions()->startSlugSuffixFrom(2);
}
};

$model->name = 'this is a test';
$model->save();

$replica = $model->replicate();
$replica->save();

expect($model->url)->toEqual('this-is-a-test');
expect($replica->url)->toEqual('this-is-a-test-2');
});

0 comments on commit 8d42bbf

Please sign in to comment.