|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Larafolio\Models; |
|
4
|
|
|
|
|
5
|
|
|
use Larafolio\Helpers\Sluggable; |
|
6
|
|
|
use Larafolio\Models\ContentTraits\HasLinks; |
|
7
|
|
|
use Larafolio\Models\ContentTraits\HasBlocks; |
|
8
|
|
|
use Larafolio\Models\ContentTraits\HasImages; |
|
9
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
10
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes; |
|
11
|
|
|
|
|
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() |
|
45
|
|
|
{ |
|
46
|
|
|
return 'slug'; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Bootstrap model. |
|
51
|
|
|
*/ |
|
52
|
|
|
public static function boot() |
|
53
|
|
|
{ |
|
54
|
|
|
parent::boot(); |
|
55
|
|
|
|
|
56
|
|
|
static::creating(function (Model $model) { |
|
57
|
|
|
$model->setSlug('name'); |
|
58
|
|
|
}); |
|
59
|
|
|
|
|
60
|
|
|
static::updating(function (Model $model) { |
|
61
|
|
|
$model->setSlug('name'); |
|
62
|
|
|
}); |
|
63
|
|
|
} |
|
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) |
|
104
|
|
|
{ |
|
105
|
|
|
$items = $this->{$relationship}->where('name', $name); |
|
106
|
|
|
|
|
107
|
|
|
if ($items->isEmpty()) { |
|
108
|
|
|
return; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
return $items->first(); |
|
112
|
|
|
} |
|
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) |
|
125
|
|
|
{ |
|
126
|
|
|
return static::join($table, "{$modelTable}.id", '=', "{$table}.resource_id") |
|
127
|
|
|
->where("{$table}.name", '=', $name) |
|
128
|
|
|
->where("{$table}.resource_type", '=', $class) |
|
129
|
|
|
->select("{$modelTable}.*") |
|
130
|
|
|
->get(); |
|
131
|
|
|
} |
|
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) |
|
142
|
|
|
{ |
|
143
|
|
|
return $query->orderRelationship('blocks') |
|
144
|
|
|
->where('slug', $slug); |
|
145
|
|
|
} |
|
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) |
|
156
|
|
|
{ |
|
157
|
|
|
return $query->orderRelationship('blocks') |
|
158
|
|
|
->orderRelationship('links') |
|
159
|
|
|
->where('slug', $slug); |
|
160
|
|
|
} |
|
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) |
|
171
|
|
|
{ |
|
172
|
|
|
return $query->with([$relationship => function ($query) { |
|
173
|
|
|
$query->orderBy('order'); |
|
174
|
|
|
}]); |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* Return page properties to be passed to js. |
|
179
|
|
|
* |
|
180
|
|
|
* @return array |
|
181
|
|
|
*/ |
|
182
|
|
|
public function generateProps() |
|
183
|
|
|
{ |
|
184
|
|
|
return [ |
|
185
|
|
|
'deletedAt' => $this->deleted_at->diffForHumans(), |
|
186
|
|
|
'id' => $this->id(), |
|
|
|
|
|
|
187
|
|
|
'name' => $this->name(), |
|
|
|
|
|
|
188
|
|
|
'slug' => $this->slug(), |
|
|
|
|
|
|
189
|
|
|
]; |
|
190
|
|
|
} |
|
191
|
|
|
} |
|
192
|
|
|
|
If you implement
__calland 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
__callis implemented by a parent class and only the child class knows which methods exist: