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
|
|
View Code Duplication |
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 blocks sorted by order. |
116
|
|
|
* |
117
|
|
|
* @param Illuminate\Database\Eloquent\Builder $query Query builder. |
118
|
|
|
* @param string $slug Project slug. |
119
|
|
|
* |
120
|
|
|
* @return Illuminate\Database\Eloquent\Builder |
121
|
|
|
*/ |
122
|
|
|
public function scopeWithBlocks($query, $slug) |
123
|
|
|
{ |
124
|
|
|
return $query->orderRelationship('blocks') |
125
|
|
|
->where('slug', $slug); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Get full model info (blocks and links sorted by order). |
130
|
|
|
* |
131
|
|
|
* @param Illuminate\Database\Eloquent\Builder $query Query builder. |
132
|
|
|
* @param string $slug Project slug. |
133
|
|
|
* |
134
|
|
|
* @return Illuminate\Database\Eloquent\Builder |
135
|
|
|
*/ |
136
|
|
|
public function scopeFull($query, $slug) |
137
|
|
|
{ |
138
|
|
|
return $query->orderRelationship('blocks') |
139
|
|
|
->orderRelationship('links') |
140
|
|
|
->where('slug', $slug); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Order given relationship by order value. |
145
|
|
|
* |
146
|
|
|
* @param Illuminate\Database\Eloquent\Builder $query Query builder. |
147
|
|
|
* @param string $relationship Name of relationship to order. |
148
|
|
|
* |
149
|
|
|
* @return Illuminate\Database\Eloquent\Builder |
150
|
|
|
*/ |
151
|
|
|
public function scopeOrderRelationship($query, $relationship) |
152
|
|
|
{ |
153
|
|
|
return $query->with([$relationship => function ($query) { |
154
|
|
|
$query->orderBy('order'); |
155
|
|
|
}]); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Return page properties to be passed to js. |
160
|
|
|
* |
161
|
|
|
* @return array |
162
|
|
|
*/ |
163
|
|
|
public function generateProps() |
164
|
|
|
{ |
165
|
|
|
return [ |
166
|
|
|
'deletedAt' => $this->deleted_at->diffForHumans(), |
|
|
|
|
167
|
|
|
'id' => $this->id(), |
|
|
|
|
168
|
|
|
'name' => $this->name(), |
|
|
|
|
169
|
|
|
'slug' => $this->slug(), |
|
|
|
|
170
|
|
|
]; |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.