|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Cviebrock\EloquentSluggable; |
|
4
|
|
|
|
|
5
|
|
|
use Cviebrock\EloquentSluggable\Sluggable; |
|
6
|
|
|
|
|
7
|
|
|
trait PrimarySlug { |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Primary slug column of this model. |
|
11
|
|
|
* |
|
12
|
|
|
* @return string |
|
13
|
|
|
*/ |
|
14
|
|
|
public function primarySlug(){ |
|
15
|
|
|
if(isset($this->primarySlug)){ |
|
16
|
|
|
return $this->primarySlug; |
|
|
|
|
|
|
17
|
|
|
} |
|
18
|
|
|
return 'slug'; |
|
19
|
|
|
} |
|
20
|
|
|
// |
|
21
|
|
|
// /** |
|
22
|
|
|
// * how to map the primary slug to the router |
|
23
|
|
|
// * |
|
24
|
|
|
// * @return string |
|
25
|
|
|
// */ |
|
26
|
|
|
// public function getRouteKeyName() { |
|
27
|
|
|
// return $this->primarySlug(); |
|
28
|
|
|
// } |
|
29
|
|
|
// |
|
30
|
|
|
// /** |
|
31
|
|
|
// * Return the sluggable configuration array for this model. |
|
32
|
|
|
// * |
|
33
|
|
|
// * @return array |
|
34
|
|
|
// */ |
|
35
|
|
|
// public function sluggable() { |
|
36
|
|
|
// $config = (method_exists(get_parent_class($this), 'getConfig')) ? parent::sluggable() : []; |
|
37
|
|
|
// return array_merge($config, [ |
|
38
|
|
|
// $this->primarySlug() => $this->primarySlugConfig() |
|
39
|
|
|
// ]); |
|
40
|
|
|
// } |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Query scope for finding a model by its slug. |
|
44
|
|
|
* @param $scope |
|
45
|
|
|
* @param $slug |
|
46
|
|
|
* @return mixed |
|
47
|
|
|
*/ |
|
48
|
|
|
public function scopeWhereSlug($scope, $slug) { |
|
49
|
|
|
return $scope->where($this->primarySlug(), $slug); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Find a model by slug. |
|
54
|
|
|
* @param $slug |
|
55
|
|
|
* @return \Illuminate\Database\Eloquent\Model|null |
|
56
|
|
|
*/ |
|
57
|
|
|
public static function findBySlug($slug) { |
|
58
|
|
|
return static::whereSlug($slug)->first(); |
|
|
|
|
|
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Find a model by slug or fail. |
|
63
|
|
|
* @param $slug |
|
64
|
|
|
* @return \Illuminate\Database\Eloquent\Model |
|
65
|
|
|
*/ |
|
66
|
|
|
public static function findBySlugOrFail($slug) { |
|
67
|
|
|
return static::whereSlug($slug)->firstOrFail(); |
|
|
|
|
|
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Simple find by Id if it's numeric or slug if not. Fail if not found. |
|
72
|
|
|
* @param $slug |
|
73
|
|
|
* @return \Illuminate\Database\Eloquent\Model|\Illuminate\Support\Collection |
|
74
|
|
|
*/ |
|
75
|
|
|
public static function findBySlugOrIdOrFail($slug) { |
|
76
|
|
|
return static::findBySlug($slug) ?: static::findOrFail((int)$slug); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Simple find by Id if it's numeric or slug if not. |
|
81
|
|
|
* @param $slug |
|
82
|
|
|
* @return \Illuminate\Database\Eloquent\Model|\Illuminate\Support\Collection|null |
|
83
|
|
|
*/ |
|
84
|
|
|
public static function findBySlugOrId($slug) { |
|
85
|
|
|
return static::findBySlug($slug) ?: static::find($slug); |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: