Skip to content

Commit 8d42bbf

Browse files
authored
Merge pull request #249 from Vediovis/main
Allow set slug suffix starting number
2 parents 149ddfb + 8ff11e1 commit 8d42bbf

File tree

4 files changed

+42
-1
lines changed

4 files changed

+42
-1
lines changed

README.md

+14
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,20 @@ public function getSlugOptions() : SlugOptions
297297
}
298298
```
299299

300+
### Setting the slug suffix starting index
301+
302+
By default, suffix index starts from 1, you can set starting number.
303+
304+
```php
305+
public function getSlugOptions() : SlugOptions
306+
{
307+
return SlugOptions::create()
308+
->generateSlugsFrom('name')
309+
->saveSlugsTo('slug')
310+
->startSlugSuffixFrom(2);
311+
}
312+
```
313+
300314
### Integration with laravel-translatable
301315

302316
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.

src/HasSlug.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ protected function getSlugSourceStringFromCallable(): string
128128
protected function makeSlugUnique(string $slug): string
129129
{
130130
$originalSlug = $slug;
131-
$i = 1;
131+
$i = $this->slugOptions->startSlugSuffixFrom;
132132

133133
while ($this->otherRecordExistsWithSlug($slug) || $slug === '') {
134134
$slug = $originalSlug.$this->slugOptions->slugSeparator.$i++;

src/SlugOptions.php

+9
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ class SlugOptions
3030

3131
public array $translatableLocales = [];
3232

33+
public int $startSlugSuffixFrom = 1;
34+
3335
public static function create(): static
3436
{
3537
return new static();
@@ -124,4 +126,11 @@ public function extraScope(callable $callbackMethod): self
124126

125127
return $this;
126128
}
129+
130+
public function startSlugSuffixFrom(int $startSlugSuffixFrom): self
131+
{
132+
$this->startSlugSuffixFrom = max(1, $startSlugSuffixFrom);
133+
134+
return $this;
135+
}
127136
}

tests/HasSlugTest.php

+18
Original file line numberDiff line numberDiff line change
@@ -315,3 +315,21 @@ public function getSlugOptions(): SlugOptions
315315
expect($model->url)->toEqual('this-is-a-test');
316316
expect($replica->url)->toEqual('this-is-a-test-1');
317317
});
318+
319+
it('can generate slug suffix starting from given number', function () {
320+
$model = new class () extends TestModel {
321+
public function getSlugOptions(): SlugOptions
322+
{
323+
return parent::getSlugOptions()->startSlugSuffixFrom(2);
324+
}
325+
};
326+
327+
$model->name = 'this is a test';
328+
$model->save();
329+
330+
$replica = $model->replicate();
331+
$replica->save();
332+
333+
expect($model->url)->toEqual('this-is-a-test');
334+
expect($replica->url)->toEqual('this-is-a-test-2');
335+
});

0 commit comments

Comments
 (0)