|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Models; |
|
4
|
|
|
|
|
5
|
|
|
use Backpack\CRUD\app\Models\Traits\CrudTrait; |
|
6
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
7
|
|
|
use Illuminate\Support\Str; |
|
8
|
|
|
use Spatie\Permission\Traits\HasRoles; |
|
9
|
|
|
|
|
10
|
|
|
class Monster extends Model |
|
11
|
|
|
{ |
|
12
|
|
|
use CrudTrait; |
|
|
|
|
|
|
13
|
|
|
use HasRoles; |
|
|
|
|
|
|
14
|
|
|
|
|
15
|
|
|
/* |
|
16
|
|
|
|-------------------------------------------------------------------------- |
|
17
|
|
|
| GLOBAL VARIABLES |
|
18
|
|
|
|-------------------------------------------------------------------------- |
|
19
|
|
|
*/ |
|
20
|
|
|
|
|
21
|
|
|
protected $table = 'monsters'; |
|
22
|
|
|
protected $primaryKey = 'id'; |
|
23
|
|
|
public $timestamps = true; |
|
24
|
|
|
// protected $guarded = ['id']; |
|
25
|
|
|
protected $fillable = ['address_algolia', 'base64_image', 'browse', 'browse_multiple', 'checkbox', 'wysiwyg', 'color', 'color_picker', 'date', 'date_picker', 'start_date', 'end_date', 'datetime', 'datetime_picker', 'email', 'hidden', 'icon_picker', 'image', 'month', 'number', 'float', 'password', 'radio', 'range', 'select', 'select_from_array', 'select2', 'select2_from_ajax', 'select2_from_array', 'simplemde', 'summernote', 'table', 'textarea', 'text', 'tinymce', 'upload', 'upload_multiple', 'url', 'video', 'week', 'extras', 'icon_id']; |
|
26
|
|
|
// protected $hidden = []; |
|
27
|
|
|
// protected $dates = []; |
|
28
|
|
|
protected $casts = [ |
|
29
|
|
|
'address_algolia' => 'object', |
|
30
|
|
|
'video' => 'array', |
|
31
|
|
|
'upload_multiple' => 'array', |
|
32
|
|
|
'browse_multiple' => 'array', |
|
33
|
|
|
// optional casts for select from array fields that allow multiple selection |
|
34
|
|
|
// 'select_from_array' => 'array', |
|
35
|
|
|
// 'select2_from_array' => 'array' |
|
36
|
|
|
]; |
|
37
|
|
|
|
|
38
|
|
|
/* |
|
39
|
|
|
|-------------------------------------------------------------------------- |
|
40
|
|
|
| FUNCTIONS |
|
41
|
|
|
|-------------------------------------------------------------------------- |
|
42
|
|
|
*/ |
|
43
|
|
|
|
|
44
|
|
|
public function openGoogle($crud = false) |
|
|
|
|
|
|
45
|
|
|
{ |
|
46
|
|
|
return '<a class="btn btn-sm btn-link" target="_blank" href="http://google.com?q='.urlencode($this->text).'" data-toggle="tooltip" title="Just a demo custom button."><i class="la la-search"></i> Google it</a>'; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public function getCategory() |
|
50
|
|
|
{ |
|
51
|
|
|
return $this->category; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/* |
|
55
|
|
|
|-------------------------------------------------------------------------- |
|
56
|
|
|
| RELATIONS |
|
57
|
|
|
|-------------------------------------------------------------------------- |
|
58
|
|
|
*/ |
|
59
|
|
|
|
|
60
|
|
|
public function article() |
|
61
|
|
|
{ |
|
62
|
|
|
return $this->belongsTo(\Backpack\NewsCRUD\app\Models\Article::class, 'select2_from_ajax'); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
public function articles() |
|
66
|
|
|
{ |
|
67
|
|
|
return $this->belongsToMany(\Backpack\NewsCRUD\app\Models\Article::class, 'monster_article'); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
public function category() |
|
71
|
|
|
{ |
|
72
|
|
|
return $this->belongsTo(\Backpack\NewsCRUD\app\Models\Category::class, 'select'); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
public function categories() |
|
76
|
|
|
{ |
|
77
|
|
|
return $this->belongsToMany(\Backpack\NewsCRUD\app\Models\Category::class, 'monster_category'); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
public function tags() |
|
81
|
|
|
{ |
|
82
|
|
|
return $this->belongsToMany(\Backpack\NewsCRUD\app\Models\Tag::class, 'monster_tag'); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
public function icon() |
|
86
|
|
|
{ |
|
87
|
|
|
return $this->belongsTo(\App\Models\Icon::class, 'icon_id'); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
public function icondummy() |
|
91
|
|
|
{ |
|
92
|
|
|
return $this->belongsTo(\App\Models\Icon::class, 'belongs_to_non_nullable'); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
public function products() |
|
96
|
|
|
{ |
|
97
|
|
|
return $this->belongsToMany(\App\Models\Product::class, 'monster_product'); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
public function dummyproducts() |
|
101
|
|
|
{ |
|
102
|
|
|
return $this->belongsToMany(\App\Models\Product::class, 'monster_productdummy')->withPivot('notes'); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
public function address() |
|
106
|
|
|
{ |
|
107
|
|
|
return $this->hasOne(\App\Models\Address::class); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
public function postalboxes() |
|
111
|
|
|
{ |
|
112
|
|
|
return $this->hasMany(\App\Models\PostalBox::class); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
public function postalboxer() |
|
116
|
|
|
{ |
|
117
|
|
|
return $this->hasMany(\App\Models\PostalBoxer::class); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
public function comment() |
|
121
|
|
|
{ |
|
122
|
|
|
return $this->morphOne(\App\Models\Comment::class, 'commentable'); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
public function recommends() |
|
126
|
|
|
{ |
|
127
|
|
|
return $this->morphToMany(\App\Models\Recommend::class, 'recommendable')->withPivot('text'); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
public function bills() |
|
131
|
|
|
{ |
|
132
|
|
|
return $this->morphToMany(\App\Models\Bill::class, 'billable'); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
public function stars() |
|
136
|
|
|
{ |
|
137
|
|
|
return $this->morphMany(\App\Models\Star::class, 'starable'); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/* |
|
141
|
|
|
|-------------------------------------------------------------------------- |
|
142
|
|
|
| SCOPES |
|
143
|
|
|
|-------------------------------------------------------------------------- |
|
144
|
|
|
*/ |
|
145
|
|
|
|
|
146
|
|
|
/* |
|
147
|
|
|
|-------------------------------------------------------------------------- |
|
148
|
|
|
| ACCESORS |
|
149
|
|
|
|-------------------------------------------------------------------------- |
|
150
|
|
|
*/ |
|
151
|
|
|
|
|
152
|
|
|
public function getTextAndEmailAttribute() |
|
153
|
|
|
{ |
|
154
|
|
|
return $this->text.' '.$this->email; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/* |
|
158
|
|
|
|-------------------------------------------------------------------------- |
|
159
|
|
|
| MUTATORS |
|
160
|
|
|
|-------------------------------------------------------------------------- |
|
161
|
|
|
*/ |
|
162
|
|
|
|
|
163
|
|
|
public function setBase64ImageAttribute($value) |
|
164
|
|
|
{ |
|
165
|
|
|
if (app('env') == 'production') { |
|
166
|
|
|
\Alert::warning('In the online demo the base64 images don\'t get stored.'); |
|
|
|
|
|
|
167
|
|
|
|
|
168
|
|
|
return true; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
$this->attributes['base64_image'] = $value; |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
public function setImageAttribute($value) |
|
175
|
|
|
{ |
|
176
|
|
|
if (app('env') == 'production') { |
|
177
|
|
|
\Alert::warning('In the online demo the images don\'t get uploaded.'); |
|
178
|
|
|
|
|
179
|
|
|
return true; |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
$attribute_name = 'image'; |
|
183
|
|
|
$disk = 'public'; // use Backpack's root disk; or your own |
|
184
|
|
|
$destination_path = 'uploads/monsters/image_field'; |
|
185
|
|
|
|
|
186
|
|
|
// if the image was erased |
|
187
|
|
|
if ($value == null) { |
|
188
|
|
|
// delete the image from disk |
|
189
|
|
|
\Storage::disk($disk)->delete($this->{$attribute_name}); |
|
190
|
|
|
|
|
191
|
|
|
// set null in the database column |
|
192
|
|
|
$this->attributes[$attribute_name] = null; |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
// if a base64 was sent, store it in the db |
|
196
|
|
|
if (Str::startsWith($value, 'data:image')) { |
|
197
|
|
|
// 0. Make the image |
|
198
|
|
|
$image = \Image::make($value)->encode('jpg', 90); |
|
199
|
|
|
|
|
200
|
|
|
// 1. Generate a filename. |
|
201
|
|
|
$filename = md5($value.time()).'.jpg'; |
|
202
|
|
|
|
|
203
|
|
|
// 2. Store the image on disk. |
|
204
|
|
|
\Storage::disk($disk)->put($destination_path.'/'.$filename, $image->stream()); |
|
205
|
|
|
|
|
206
|
|
|
// 3. Delete the previous image, if there was one. |
|
207
|
|
|
\Storage::disk($disk)->delete($this->{$attribute_name}); |
|
208
|
|
|
|
|
209
|
|
|
// 4. Save the public path to the database |
|
210
|
|
|
// but first, remove "public/" from the path, since we're pointing to it from the root folder |
|
211
|
|
|
// that way, what gets saved in the database is the user-accesible URL |
|
212
|
|
|
$public_destination_path = Str::replaceFirst('public/', '', $destination_path); |
|
213
|
|
|
$this->attributes[$attribute_name] = $public_destination_path.'/'.$filename; |
|
214
|
|
|
} |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
public function setUploadAttribute($value) |
|
218
|
|
|
{ |
|
219
|
|
|
if (app('env') == 'production') { |
|
220
|
|
|
\Alert::warning('In the online demo the files don\'t get uploaded.'); |
|
221
|
|
|
|
|
222
|
|
|
return true; |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
$attribute_name = 'upload'; |
|
226
|
|
|
$disk = 'public'; |
|
227
|
|
|
$destination_path = 'uploads/monsters/upload_field'; |
|
228
|
|
|
|
|
229
|
|
|
$this->uploadFileToDisk($value, $attribute_name, $disk, $destination_path); |
|
230
|
|
|
|
|
231
|
|
|
// return $this->attributes[{$attribute_name}]; // uncomment if this is a translatable field |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
|
|
public function setUploadMultipleAttribute($value) |
|
235
|
|
|
{ |
|
236
|
|
|
if (app('env') == 'production') { |
|
237
|
|
|
\Alert::warning('In the online demo the files don\'t get uploaded.'); |
|
238
|
|
|
|
|
239
|
|
|
return true; |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
$attribute_name = 'upload_multiple'; |
|
243
|
|
|
$disk = 'public'; |
|
244
|
|
|
$destination_path = 'uploads/monsters/upload_multiple_field'; |
|
245
|
|
|
|
|
246
|
|
|
$this->uploadMultipleFilesToDisk($value, $attribute_name, $disk, $destination_path); |
|
247
|
|
|
} |
|
248
|
|
|
} |
|
249
|
|
|
|