1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Backpack\CRUD\ModelTraits\SpatieTranslatable; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
6
|
|
|
use Illuminate\Database\Eloquent\Builder; |
7
|
|
|
|
8
|
|
|
trait Sluggable |
9
|
|
|
{ |
10
|
|
|
use \Cviebrock\EloquentSluggable\Sluggable; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Hook into the Eloquent model events to create or |
14
|
|
|
* update the slug as required. |
15
|
|
|
*/ |
16
|
|
|
public static function bootSluggable() |
17
|
|
|
{ |
18
|
|
|
static::observe(app(SluggableObserver::class)); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Clone the model into a new, non-existing instance. |
23
|
|
|
* |
24
|
|
|
* @param array|null $except |
25
|
|
|
* @return Model |
26
|
|
|
*/ |
27
|
|
|
public function replicate(array $except = null) |
28
|
|
|
{ |
29
|
|
|
$instance = parent::replicate($except); |
30
|
|
|
(new SlugService())->slug($instance, true); |
31
|
|
|
|
32
|
|
|
return $instance; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Query scope for finding "similar" slugs, used to determine uniqueness. |
37
|
|
|
* |
38
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
39
|
|
|
* @param \Illuminate\Database\Eloquent\Model $model |
40
|
|
|
* @param string $attribute |
41
|
|
|
* @param array $config |
42
|
|
|
* @param string $slug |
43
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
44
|
|
|
*/ |
45
|
|
|
public function scopeFindSimilarSlugs(Builder $query, Model $model, $attribute, $config, $slug) |
|
|
|
|
46
|
|
|
{ |
47
|
|
|
$separator = $config['separator']; |
48
|
|
|
$attribute = $attribute.'->'.$this->getLocale(); |
49
|
|
|
|
50
|
|
|
return $query->where(function (Builder $q) use ($attribute, $slug, $separator) { |
51
|
|
|
$q->where($attribute, '=', $slug) |
52
|
|
|
->orWhere($attribute, 'LIKE', $slug.$separator.'%'); |
53
|
|
|
}); |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.