Skip to content

Commit 9667df0

Browse files
authored
Merge pull request #8 from yabhq/feature/slug-key-name-change
Allow the slugify key name to be changed
2 parents e524aab + 951e3de commit 9667df0

File tree

2 files changed

+48
-5
lines changed

2 files changed

+48
-5
lines changed

README.md

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,12 @@ Save a model "quietly" without broadcasting any events or firing off any observe
1818
```php
1919
use Yab\Mint\Traits\SavesQuietly;
2020

21-
class Example extends Model
21+
class Example extends Model
2222
{
2323
use SavesQuietly;
2424
}
2525
```
26+
2627
```php
2728
$example->saveQuietly();
2829
```
@@ -34,11 +35,12 @@ Allow for models to be archived or unarchived based on an "archived_at" field on
3435
```php
3536
use Yab\Mint\Traits\Archivable;
3637

37-
class Example extends Model
38+
class Example extends Model
3839
{
3940
use Archivable;
4041
}
4142
```
43+
4244
```php
4345
$example->archive();
4446
$example->unarchive();
@@ -54,11 +56,12 @@ Models which are marked as "immutable" will throw an ImmutableDataException if u
5456
```php
5557
use Yab\Mint\Traits\Immutable;
5658

57-
class Example extends Model
59+
class Example extends Model
5860
{
5961
use Immutable;
6062
}
6163
```
64+
6265
```php
6366
// No problem
6467
$example = Example::create([
@@ -101,10 +104,40 @@ A custom cast for storing monetary values as cents in the database while fetchin
101104
```php
102105
use Yab\Mint\Casts\Money;
103106

104-
class Example extends Model
107+
class Example extends Model
105108
{
106109
protected $casts = [
107110
'price' => Money::class,
108111
];
109112
}
110113
```
114+
115+
### Slugify
116+
117+
Create slugs that are unique and never collide with each other
118+
119+
```php
120+
use Yab\Mint\Trails\Slugify;
121+
122+
class Example extends Model
123+
{
124+
use Slugify
125+
}
126+
```
127+
128+
By default the Slugify trait uses the `name` property on your model. You can change this
129+
by overriding the `getSlugKeyName` method.
130+
131+
```php
132+
use Yab\Mint\Trails\Slugify;
133+
134+
class Example extends Model
135+
{
136+
use Slugify;
137+
138+
public static function getSlugKeyName(): string
139+
{
140+
return 'title';
141+
}
142+
}
143+
```

src/Traits/Slugify.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public static function bootSlugify()
1111
{
1212
static::creating(function ($model) {
1313
if (is_null($model->slug)) {
14-
$model->slug = Str::slug($model->name);
14+
$model->slug = Str::slug($model->{self::getSlugKeyName()});
1515
}
1616
$model->slug = self::checkModelSlug($model->slug);
1717
});
@@ -63,6 +63,16 @@ public function scopeBySlug(Builder $scope, string $slug): Builder
6363
return $scope->where('slug', $slug);
6464
}
6565

66+
/**
67+
* Set the model property that will be used to create the slug
68+
*
69+
* @return string
70+
*/
71+
public static function getSlugKeyName(): string
72+
{
73+
return 'name';
74+
}
75+
6676
/**
6777
* Get the route key for the model.
6878
*

0 commit comments

Comments
 (0)