You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: README.md
+36-3
Original file line number
Diff line number
Diff line change
@@ -30,7 +30,8 @@ We highly appreciate you sending us a postcard from your hometown, mentioning wh
30
30
## Installation
31
31
32
32
You can install the package via composer:
33
-
```bash
33
+
34
+
```bash
34
35
composer require spatie/laravel-sluggable
35
36
```
36
37
@@ -281,7 +282,6 @@ public function getSlugOptions() : SlugOptions
281
282
282
283
### Using scopes
283
284
284
-
285
285
If you have a global scope that should be taken into account, you can define this as well with `extraScope`. For example if you have a pages table containing pages of multiple websites and every website has it's own unique slugs.
286
286
287
287
```php
@@ -308,6 +308,40 @@ public function getSlugOptions() : SlugOptions
308
308
}
309
309
```
310
310
311
+
### Generating slug suffix on first occurrence
312
+
313
+
With the default behavior (assuming that we haven't disabled slug uniqueness with `allowDuplicateSlugs`), the generated slugs for two records with the same source values would be `this-is-an-example` and `this-is-an-example-1`.
314
+
315
+
When using this option, we are forcing the first occurence to also have a suffix so, even if the slug is unique as it is, it will be suffixed, resulting in `this-is-an-example-1` and `this-is-an-example-2`.
316
+
317
+
```php
318
+
public function getSlugOptions() : SlugOptions
319
+
{
320
+
return SlugOptions::create()
321
+
->generateSlugsFrom('name')
322
+
->saveSlugsTo('slug')
323
+
->useSuffixOnFirstOccurrence();
324
+
}
325
+
```
326
+
327
+
### Generating a custom slug suffix
328
+
329
+
By default, the mechanism to make slugs unique is to append an autoincremental value to the slug. You can generate a custom slug suffix such as a random string or hash with `usingSuffixGenerator`.
330
+
331
+
It accepts a callable that receives the base slug (without any suffix) and the iteration number, which represents how many times the suffix generation process has been run to ensure uniqueness. This number could be useful to monitor the collision rate of the generation process.
332
+
333
+
```php
334
+
public function getSlugOptions() : SlugOptions
335
+
{
336
+
return SlugOptions::create()
337
+
->generateSlugsFrom('name')
338
+
->saveSlugsTo('slug')
339
+
->usingSuffixGenerator(
340
+
fn(string $slug, int $iteration) => bin2hex(random_bytes(4))
341
+
); // Sample dummy method to generate a random hex code of length 8
342
+
}
343
+
```
344
+
311
345
### Integration with laravel-translatable
312
346
313
347
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.
0 commit comments