-
Notifications
You must be signed in to change notification settings - Fork 0
/
Product.php
75 lines (63 loc) · 2.07 KB
/
Product.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Cviebrock\EloquentSluggable\SluggableInterface;
use Cviebrock\EloquentSluggable\SluggableTrait;
class Product extends Model implements SluggableInterface
{
use SluggableTrait;
protected $sluggable = [
'build_from' => 'title',
'save_to' => 'slug',
];
protected $table = 'products';
protected $fillable = ['title', 'subtitle', 'thumbnail', 'description', 'specs', 'details', 'images', 'tags', 'price', 'link', 'category_id', 'product_id'];
public function colors() {
return $this->belongsToMany('App\Color', 'product_colors');
}
public function category() {
return $this->belongsTo('App\Category');
}
public function parent() {
return $this->belongsTo('App\Product');
}
// public function getSpecsAttribute($value)
public function setSpecsAttribute($value) {
$cadena = json_encode($value);
$this->attributes['specs'] = $cadena;
}
public function getSpecsAttribute($value) {
return json_decode($value);
}
public function getImagesAttribute($value)
{
$images = preg_split('/,/', $value, -1, PREG_SPLIT_NO_EMPTY);
if (! is_array($images) ) $images =[];
return $images;
}
public function setImagesAttribute($images)
{
$cadena = implode(',', $images);
$this->attributes['images'] = $cadena;
}
public function getImagesCuratedAttribute() {
$result = array();
foreach($this->images as $image) {
$result[] = str_replace('uploads/', '', $image);
}
return $result;
}
public function getThumbnailCuratedAttribute() {
return str_replace('uploads/', '', $this->thumbnail);
}
public function getUrlAttribute() {
$url = "/productos/{$this->category->slug}/{$this->slug}";
return $url;
}
public function setColorsAttribute($colors) {
$this->colors()->detach();
if (! $colors) return;
if (! $this->exists) $this->save();
$this->colors()->attach($colors);
}
}