1 | <?php |
||
12 | class HasContent extends Model |
||
13 | { |
||
14 | use HasBlocks, HasImages, HasLinks, Sluggable, SoftDeletes; |
||
15 | |||
16 | /** |
||
17 | * Properties to always eager load. |
||
18 | * |
||
19 | * @var array |
||
20 | */ |
||
21 | protected $with = ['blocks', 'images', 'links']; |
||
22 | |||
23 | /** |
||
24 | * The attributes that should be casted to native types. |
||
25 | * |
||
26 | * @var array |
||
27 | */ |
||
28 | protected $casts = [ |
||
29 | 'visible' => 'boolean', |
||
30 | ]; |
||
31 | |||
32 | /** |
||
33 | * Fields that are dates. |
||
34 | * |
||
35 | * @var array |
||
36 | */ |
||
37 | protected $dates = ['created_at', 'updated_at', 'deleted_at']; |
||
38 | |||
39 | /** |
||
40 | * Get the route key for the model. |
||
41 | * |
||
42 | * @return string |
||
43 | */ |
||
44 | public function getRouteKeyName() |
||
48 | |||
49 | /** |
||
50 | * Bootstrap model. |
||
51 | */ |
||
52 | public static function boot() |
||
64 | |||
65 | /** |
||
66 | * Order and group query, return results. |
||
67 | * |
||
68 | * @param \Illuminate\Database\Eloquent\Builder $query Query to be ordered. |
||
69 | * @param bool $group If true, group projects by 'type'. |
||
70 | * @param bool $order If true, order projects by 'order'. |
||
71 | * |
||
72 | * @return \Illuminate\Support\Collection |
||
73 | */ |
||
74 | protected static function orderAndGroupQuery($query, $group, $order) |
||
75 | { |
||
76 | if ($order) { |
||
77 | $query->orderBy('order'); |
||
78 | } |
||
79 | |||
80 | $query->orderRelationship('links'); |
||
81 | |||
82 | $query->orderRelationship('blocks'); |
||
83 | |||
84 | if ($group) { |
||
85 | return $query->get() |
||
86 | ->each(function (Model $model, $key) { |
||
87 | $model->index = $key; |
||
88 | }) |
||
89 | ->groupBy('type'); |
||
90 | } |
||
91 | |||
92 | return $query->get(); |
||
93 | } |
||
94 | |||
95 | /** |
||
96 | * Get a model from a relationship by model name. |
||
97 | * |
||
98 | * @param string $relationship Name of relationship. |
||
99 | * @param string $name Name of model to get. |
||
100 | * |
||
101 | * @return \Illuminate\Database\Eloquent\Model|null |
||
102 | */ |
||
103 | protected function getFromRelationshipByName($relationship, $name) |
||
113 | |||
114 | /** |
||
115 | * Get all models with relationship on table that have given name. |
||
116 | * |
||
117 | * @param string $class Model class name. |
||
118 | * @param string $modelTable Model table name. |
||
119 | * @param string $table Name of table relationship is on. |
||
120 | * @param string $name Relationship name. |
||
121 | * |
||
122 | * @return \Illuminate\Support\Collection |
||
123 | */ |
||
124 | protected static function hasRelationshipNamed($class, $modelTable, $table, $name) |
||
132 | |||
133 | /** |
||
134 | * Get blocks sorted by order. |
||
135 | * |
||
136 | * @param Illuminate\Database\Eloquent\Builder $query Query builder. |
||
137 | * @param string $slug Project slug. |
||
138 | * |
||
139 | * @return Illuminate\Database\Eloquent\Builder |
||
140 | */ |
||
141 | public function scopeWithBlocks($query, $slug) |
||
146 | |||
147 | /** |
||
148 | * Get full model info (blocks and links sorted by order). |
||
149 | * |
||
150 | * @param Illuminate\Database\Eloquent\Builder $query Query builder. |
||
151 | * @param string $slug Project slug. |
||
152 | * |
||
153 | * @return Illuminate\Database\Eloquent\Builder |
||
154 | */ |
||
155 | public function scopeFull($query, $slug) |
||
161 | |||
162 | /** |
||
163 | * Order given relationship by order value. |
||
164 | * |
||
165 | * @param Illuminate\Database\Eloquent\Builder $query Query builder. |
||
166 | * @param string $relationship Name of relationship to order. |
||
167 | * |
||
168 | * @return Illuminate\Database\Eloquent\Builder |
||
169 | */ |
||
170 | public function scopeOrderRelationship($query, $relationship) |
||
176 | |||
177 | /** |
||
178 | * Return page properties to be passed to js. |
||
179 | * |
||
180 | * @return array |
||
181 | */ |
||
182 | public function generateProps() |
||
191 | } |
||
192 |
If you implement
__call
and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__call
is implemented by a parent class and only the child class knows which methods exist: