|
1
|
|
|
<?php namespace App\LaravelRestCms\Page; |
|
2
|
|
|
|
|
3
|
|
|
use App\LaravelRestCms\BaseModel; |
|
4
|
|
|
use App\LaravelRestCms\Page\PageDetail; |
|
5
|
|
|
use App\LaravelRestCms\Seo\Seo; |
|
6
|
|
|
use App\LaravelRestCms\Template\Template; |
|
7
|
|
|
|
|
8
|
|
|
class Page extends BaseModel { |
|
9
|
|
|
|
|
10
|
|
|
public static $searchCols = ['nav_name', 'url', 'title']; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* The database table used by the model. |
|
14
|
|
|
* |
|
15
|
|
|
* @var string |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $table = 'pages'; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* The attributes that are mass assignable. |
|
21
|
|
|
* |
|
22
|
|
|
* @var array |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $fillable = ['parent_id', 'template_id', 'nav_name', 'url', 'title', 'created_by', 'updated_by']; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* The attributes excluded from the model's JSON form. |
|
28
|
|
|
* |
|
29
|
|
|
* @var array |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $hidden = []; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Rules to validate when creating a model |
|
35
|
|
|
* |
|
36
|
|
|
* @var array |
|
37
|
|
|
*/ |
|
38
|
|
|
protected static $createRules = [ |
|
39
|
|
|
'parent_id' => 'integer', |
|
40
|
|
|
'template_id' => 'integer', |
|
41
|
|
|
'nav_name' => 'required', |
|
42
|
|
|
'url' => 'required|unique:pages', |
|
43
|
|
|
'title' => 'required', |
|
44
|
|
|
'created_by' => 'integer', |
|
45
|
|
|
'updated_by' => 'integer', |
|
46
|
|
|
]; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Indicates if the model should be attributed with created_by and updated_by |
|
50
|
|
|
* |
|
51
|
|
|
* @var bool |
|
52
|
|
|
*/ |
|
53
|
|
|
public $attirbution = true; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Joins the page_detail table |
|
57
|
|
|
* |
|
58
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany |
|
59
|
|
|
*/ |
|
60
|
|
|
public function detail() |
|
61
|
|
|
{ |
|
62
|
|
|
return $this->hasMany(PageDetail::class, 'page_id', 'id'); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Joins the templates table |
|
67
|
|
|
* |
|
68
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\hasOne |
|
69
|
|
|
*/ |
|
70
|
|
|
public function template() |
|
71
|
|
|
{ |
|
72
|
|
|
return $this->hasMany(Template::class, 'id', 'template_id'); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Joins the seo table |
|
77
|
|
|
* |
|
78
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\hasOne |
|
79
|
|
|
*/ |
|
80
|
|
|
public function seo() |
|
81
|
|
|
{ |
|
82
|
|
|
return $this->hasMany(Seo::class, 'id', 'seo_id'); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Joins the pages table |
|
87
|
|
|
* |
|
88
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\hasOne |
|
89
|
|
|
*/ |
|
90
|
|
|
public function parent() |
|
91
|
|
|
{ |
|
92
|
|
|
return $this->hasMany(Page::class, 'id', 'parent_id'); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Returns a page and associated detail and template data |
|
97
|
|
|
* |
|
98
|
|
|
* @param string $slug |
|
99
|
|
|
* @return array |
|
100
|
|
|
*/ |
|
101
|
|
|
public function showBySlug($slug) |
|
102
|
|
|
{ |
|
103
|
|
|
$data = Page::where(['url' => $slug])->with('template', 'detail.templateDetail')->firstOrFail(); |
|
104
|
|
|
|
|
105
|
|
|
return $this->packagePage($data); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Pacages a Page collection into an array for public consumption |
|
110
|
|
|
* |
|
111
|
|
|
* @param Page $data |
|
112
|
|
|
* @return array |
|
113
|
|
|
*/ |
|
114
|
|
|
protected function packagePage(Page $data) |
|
115
|
|
|
{ |
|
116
|
|
|
$seo = $data->seo->first(); |
|
|
|
|
|
|
117
|
|
|
$template = $data->template->first(); |
|
|
|
|
|
|
118
|
|
|
|
|
119
|
|
|
$page = [ |
|
120
|
|
|
'nav_name' => $data->nav_name, |
|
|
|
|
|
|
121
|
|
|
'url' => $data->url, |
|
|
|
|
|
|
122
|
|
|
'title' => $data->title, |
|
|
|
|
|
|
123
|
|
|
'vars' => [], |
|
124
|
|
|
'template' => [ |
|
125
|
|
|
'name' => $template->name, |
|
126
|
|
|
'class' => $template->class, |
|
127
|
|
|
'method' => $template->method, |
|
128
|
|
|
'params' => $template->params, |
|
129
|
|
|
'template_name' => $template->template_name, |
|
130
|
|
|
'layout' => $template->layout, |
|
131
|
|
|
], |
|
132
|
|
|
'seo' => [ |
|
133
|
|
|
'title' => $seo->title, |
|
134
|
|
|
'keywords' => $seo->keywords, |
|
135
|
|
|
'description' => $seo->description, |
|
136
|
|
|
'og_title' => $seo->og_title, |
|
137
|
|
|
'og_description' => $seo->og_description, |
|
138
|
|
|
'og_image' => $seo->og_image, |
|
139
|
|
|
'og_type' => $seo->og_type, |
|
140
|
|
|
'fb_app_id' => $seo->fb_app_id, |
|
141
|
|
|
'meta' => $seo->meta, |
|
142
|
|
|
] |
|
143
|
|
|
]; |
|
144
|
|
|
|
|
145
|
|
|
foreach ($data->detail as $detail) { |
|
|
|
|
|
|
146
|
|
|
$page['vars'][$detail->templateDetail->first()->var] = $detail->data; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
return $page; |
|
150
|
|
|
} |
|
151
|
|
|
} |
|
152
|
|
|
|
Since your code implements the magic getter
_get, this function will be called for any read access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.