-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sluggable.php
54 lines (43 loc) · 1.73 KB
/
Sluggable.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<?php
namespace LaravelReady\ModelSupport\Traits;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\Config;
use Illuminate\Database\Eloquent\Builder;
trait Sluggable
{
public function initializeSluggable(): void
{
$slugFieldName = Config::get('sluggable_fields.slug', 'slug');
$titleFieldName = Config::get('sluggable_fields.title', 'title');
static::creating(function ($model) use ($slugFieldName, $titleFieldName) {
$model->$slugFieldName = Str::slug($model->$titleFieldName);
});
static::updating(function ($model) use ($slugFieldName, $titleFieldName) {
$model->$slugFieldName = Str::slug($model->$titleFieldName);
});
}
public function scopeSlug(mixed $query, string $slug): Builder
{
return $query->where(Config::get('sluggable_fields.slug', 'slug'), $slug);
}
public function scopeSlugLike(mixed $query, string $slug): Builder
{
return $query->where(Config::get('sluggable_fields.slug', 'slug'), 'like', "%{$slug}%");
}
public function scopeSlugNot(mixed $query, string $slug): Builder
{
return $query->where(Config::get('sluggable_fields.slug', 'slug'), '!=', $slug);
}
public function scopeSlugNotLike(mixed $query, string $slug): Builder
{
return $query->where(Config::get('sluggable_fields.slug', 'slug'), 'not like', "%{$slug}%");
}
public function scopeSlugIn(mixed $query, array $slugs): Builder
{
return $query->whereIn(Config::get('sluggable_fields.slug', 'slug'), $slugs);
}
public function scopeSlugNotIn(mixed $query, array $slugs): Builder
{
return $query->whereNotIn(Config::get('sluggable_fields.slug', 'slug'), $slugs);
}
}