1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Xetaravel\Models; |
4
|
|
|
|
5
|
|
|
use Eloquence\Behaviours\CountCache\Countable; |
6
|
|
|
use Eloquence\Behaviours\Sluggable; |
7
|
|
|
use Illuminate\Support\Facades\Auth; |
8
|
|
|
use Spatie\Image\Exceptions\InvalidManipulation; |
9
|
|
|
use Spatie\Image\Manipulations; |
10
|
|
|
use Spatie\MediaLibrary\HasMedia; |
11
|
|
|
use Spatie\MediaLibrary\InteractsWithMedia; |
12
|
|
|
use Spatie\MediaLibrary\MediaCollections\Models\Media; |
13
|
|
|
use Xetaio\Mentions\Models\Traits\HasMentionsTrait; |
14
|
|
|
use Xetaravel\Models\Presenters\ShopItemPresenter; |
15
|
|
|
|
16
|
|
|
class ShopItem extends Model implements HasMedia |
17
|
|
|
{ |
18
|
|
|
use Countable; |
19
|
|
|
use Sluggable; |
20
|
|
|
use ShopItemPresenter; |
21
|
|
|
use HasMentionsTrait; |
22
|
|
|
use InteractsWithMedia; |
|
|
|
|
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* The attributes that are mass assignable. |
26
|
|
|
* |
27
|
|
|
* @var array |
28
|
|
|
*/ |
29
|
|
|
protected $fillable = [ |
30
|
|
|
'title', |
31
|
|
|
'user_id', |
32
|
|
|
'shop_category_id', |
33
|
|
|
'content', |
34
|
|
|
'price', |
35
|
|
|
'discount', |
36
|
|
|
'quantity', |
37
|
|
|
'is_display', |
38
|
|
|
'start_at', |
39
|
|
|
'end_at' |
40
|
|
|
]; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* The accessors to append to the model's array form. |
44
|
|
|
* |
45
|
|
|
* @var array |
46
|
|
|
*/ |
47
|
|
|
protected $appends = [ |
48
|
|
|
'item_url', |
49
|
|
|
|
50
|
|
|
// Media Model |
51
|
|
|
'item_icon' |
52
|
|
|
]; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* The attributes that should be mutated to dates. |
56
|
|
|
* |
57
|
|
|
* @var array |
58
|
|
|
*/ |
59
|
|
|
protected $dates = [ |
60
|
|
|
'start_at', |
61
|
|
|
'end_at' |
62
|
|
|
]; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* The "booting" method of the model. |
66
|
|
|
* |
67
|
|
|
* @return void |
68
|
|
|
*/ |
69
|
|
|
protected static function boot(): void |
70
|
|
|
{ |
71
|
|
|
parent::boot(); |
72
|
|
|
|
73
|
|
|
// Generated the slug before updating. |
74
|
|
|
static::updating(function ($model) { |
75
|
|
|
$model->generateSlug(); |
76
|
|
|
}); |
77
|
|
|
|
78
|
|
|
// Set the user id to the new item before saving it. |
79
|
|
|
static::creating(function ($model) { |
80
|
|
|
$model->user_id = Auth::id(); |
81
|
|
|
}); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Return the field to slug. |
86
|
|
|
* |
87
|
|
|
* @return string |
88
|
|
|
*/ |
89
|
|
|
public function slugStrategy(): string |
90
|
|
|
{ |
91
|
|
|
return 'title'; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Return the count cache configuration. |
96
|
|
|
* |
97
|
|
|
* @return array |
98
|
|
|
*/ |
99
|
|
|
public function countCaches(): array |
100
|
|
|
{ |
101
|
|
|
return [ |
102
|
|
|
[ |
103
|
|
|
'model' => ShopCategory::class, |
104
|
|
|
'field' => 'shop_item_count', |
105
|
|
|
'foreignKey' => 'shop_category_id', |
106
|
|
|
'key' => 'id' |
107
|
|
|
] |
108
|
|
|
]; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Get the category that owns the item. |
113
|
|
|
* |
114
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
115
|
|
|
*/ |
116
|
|
|
public function shopCategory() |
117
|
|
|
{ |
118
|
|
|
return $this->belongsTo(ShopCategory::class); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Get the users that owns the items. |
123
|
|
|
* |
124
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTomany |
125
|
|
|
*/ |
126
|
|
|
public function users() |
127
|
|
|
{ |
128
|
|
|
return $this->belongsToMany(User::class); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Check if the given user has unlocked this item. |
133
|
|
|
* |
134
|
|
|
* @param User $user The user to check. |
135
|
|
|
* |
136
|
|
|
* @return bool |
137
|
|
|
*/ |
138
|
|
|
public function hasUser(User $user): bool |
139
|
|
|
{ |
140
|
|
|
return $this->users() |
141
|
|
|
->where('user_id', $user->getKey()) |
142
|
|
|
->exists(); |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|