fomvasss /
laravel-slugmaker
| 1 | <?php |
||||||
| 2 | |||||||
| 3 | namespace Fomvasss\SlugMaker; |
||||||
| 4 | |||||||
| 5 | trait ModelHasSlug |
||||||
| 6 | { |
||||||
| 7 | public $slugSourceFields; |
||||||
| 8 | |||||||
| 9 | /** |
||||||
| 10 | * @return array |
||||||
| 11 | */ |
||||||
| 12 | public function getSlugSourceFields(): array |
||||||
| 13 | { |
||||||
| 14 | $sourceFields = config('slugmaker.default_source_fields', []); |
||||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||||
| 15 | |||||||
| 16 | return is_array($sourceFields) ? $sourceFields : [$sourceFields]; |
||||||
| 17 | } |
||||||
| 18 | |||||||
| 19 | /** |
||||||
| 20 | * @return mixed |
||||||
| 21 | */ |
||||||
| 22 | public function slug() |
||||||
| 23 | { |
||||||
| 24 | return $this->morphOne(config('slugmaker.model', \Fomvasss\SlugMaker\Models\Slug::class), 'slugable'); |
||||||
|
0 ignored issues
–
show
The function
config was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
It seems like
morphOne() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 25 | } |
||||||
| 26 | |||||||
| 27 | /** |
||||||
| 28 | * The scope for fet slug name. |
||||||
| 29 | */ |
||||||
| 30 | public function getSlugName() |
||||||
| 31 | { |
||||||
| 32 | if ($this->slug) { |
||||||
| 33 | return $this->slug->name; |
||||||
| 34 | } |
||||||
| 35 | return; |
||||||
| 36 | } |
||||||
| 37 | |||||||
| 38 | /** |
||||||
| 39 | * @param $slug |
||||||
| 40 | * @return mixed |
||||||
| 41 | */ |
||||||
| 42 | public function makeSlug($slug = '') |
||||||
| 43 | { |
||||||
| 44 | return slug_make($this, $slug); |
||||||
| 45 | } |
||||||
| 46 | |||||||
| 47 | /** |
||||||
| 48 | * Models by slugs. |
||||||
| 49 | * |
||||||
| 50 | * @param $query |
||||||
| 51 | * @param string $slug |
||||||
| 52 | * @return mixed |
||||||
| 53 | */ |
||||||
| 54 | public function scopeBySlugs($query, $slug) |
||||||
| 55 | { |
||||||
| 56 | $slugs = is_array($slug) ? $slug : [$slug]; |
||||||
| 57 | |||||||
| 58 | return $query->whereHas('slug', function ($q) use ($slugs) { |
||||||
| 59 | $q->whereIn('name', $slugs); |
||||||
| 60 | }); |
||||||
| 61 | } |
||||||
| 62 | |||||||
| 63 | /** |
||||||
| 64 | * Find first model by slug. |
||||||
| 65 | * |
||||||
| 66 | * @param $query |
||||||
| 67 | * @param string $slug |
||||||
| 68 | * @return mixed |
||||||
| 69 | */ |
||||||
| 70 | public function scopeFindBySlug($query, string $slug) |
||||||
| 71 | { |
||||||
| 72 | return $this->scopeBySlugs($query, $slug)->first(); |
||||||
| 73 | } |
||||||
| 74 | |||||||
| 75 | /** |
||||||
| 76 | * Find first model by slug or throw exciption. |
||||||
| 77 | * |
||||||
| 78 | * @param $query |
||||||
| 79 | * @param string $slug |
||||||
| 80 | * @return mixed |
||||||
| 81 | */ |
||||||
| 82 | public function scopeFindOrFailBySlug($query, string $slug) |
||||||
| 83 | { |
||||||
| 84 | return $this->scopeBySlugs($query, $slug)->firstOrFail(); |
||||||
| 85 | } |
||||||
| 86 | |||||||
| 87 | /** |
||||||
| 88 | * Get models by slugs. |
||||||
| 89 | * |
||||||
| 90 | * @param $query |
||||||
| 91 | * @param $slugs |
||||||
| 92 | * @return mixed |
||||||
| 93 | */ |
||||||
| 94 | public function scopeGetBySlugs($query, $slugs) |
||||||
| 95 | { |
||||||
| 96 | return $this->scopeBySlugs($query, $slugs)->get(); |
||||||
| 97 | } |
||||||
| 98 | |||||||
| 99 | /** |
||||||
| 100 | * Get array ids by slugs. |
||||||
| 101 | * |
||||||
| 102 | * @param $query |
||||||
| 103 | * @param array $slugs |
||||||
| 104 | * @return array |
||||||
| 105 | */ |
||||||
| 106 | public function scopeGetArrayIdsBySlugs($query, array $slugs): array |
||||||
| 107 | { |
||||||
| 108 | return $this->scopeBySlugs($query, $slugs)->pluck('id')->toArray(); |
||||||
| 109 | } |
||||||
| 110 | } |
||||||
| 111 |