1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Models\Concerns\Order; |
4
|
|
|
|
5
|
|
|
use App\Models\Branch; |
6
|
|
|
use App\Models\Customer; |
7
|
|
|
use App\Models\Item; |
8
|
|
|
use App\Models\OrderStatus; |
9
|
|
|
use App\Models\User; |
10
|
|
|
use Illuminate\Database\Eloquent\Collection; |
11
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo; |
12
|
|
|
use Illuminate\Database\Eloquent\Relations\HasMany; |
13
|
|
|
use Illuminate\Database\Eloquent\Relations\HasOne; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @property int $customer_id Foreign key of \App\Models\Customer. |
17
|
|
|
* @property-read \App\Models\Customer $customer |
18
|
|
|
* @property int|null $user_id Foreign key of \App\Models\User. |
19
|
|
|
* @property-read \App\Models\User|null $user |
20
|
|
|
* @property int|null $branch_id Foreign key of \App\Models\Branch. |
21
|
|
|
* @property-read \App\Models\Branch|null $branch |
22
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<\App\Models\OrderStatus> $statuses |
23
|
|
|
* @property-read \App\Models\OrderStatus $latestStatus |
24
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<\App\Models\Item> $items |
25
|
|
|
* |
26
|
|
|
* @see \App\Models\Order |
27
|
|
|
*/ |
28
|
|
|
trait Relation |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* Define an inverse one-to-one or many relationship with \App\Models\Customer. |
32
|
|
|
* |
33
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
34
|
|
|
*/ |
35
|
3 |
|
public function customer(): BelongsTo |
36
|
|
|
{ |
37
|
3 |
|
return $this->belongsTo(Customer::class); |
|
|
|
|
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Return \App\Models\Customer model relation value. |
42
|
|
|
* |
43
|
|
|
* @return \App\Models\Customer |
44
|
|
|
*/ |
45
|
|
|
public function getCustomerRelationValue(): Customer |
46
|
|
|
{ |
47
|
|
|
return $this->getRelationValue('customer'); |
|
|
|
|
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Set \App\Models\Customer model relation value. |
52
|
|
|
* |
53
|
|
|
* @param \App\Models\Customer $customer |
54
|
|
|
* @return $this |
55
|
|
|
*/ |
56
|
1 |
|
public function setCustomerRelationValue(Customer $customer) |
57
|
|
|
{ |
58
|
1 |
|
$this->customer()->associate($customer); |
59
|
|
|
|
60
|
1 |
|
return $this; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Define an inverse one-to-one or many relationship with \App\Models\User. |
65
|
|
|
* |
66
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
67
|
|
|
*/ |
68
|
|
|
public function user(): BelongsTo |
69
|
|
|
{ |
70
|
|
|
return $this->belongsTo(User::class); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Return \App\Models\User model relation value. |
75
|
|
|
* |
76
|
|
|
* @return \App\Models\User|null |
77
|
|
|
*/ |
78
|
|
|
public function getUserRelationValue(): ?User |
79
|
|
|
{ |
80
|
|
|
return $this->getRelationValue('user'); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Set \App\Models\User model relation value. |
85
|
|
|
* |
86
|
|
|
* @param \App\Models\User $user |
87
|
|
|
* @return $this |
88
|
|
|
*/ |
89
|
|
|
public function setUserRelationValue(User $user) |
90
|
|
|
{ |
91
|
|
|
$this->user()->associate($user); |
92
|
|
|
|
93
|
|
|
return $this; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Define an inverse one-to-one or many relationship with \App\Models\Branch. |
98
|
|
|
* |
99
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
100
|
|
|
*/ |
101
|
|
|
public function branch(): BelongsTo |
102
|
|
|
{ |
103
|
|
|
return $this->belongsTo(Branch::class); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Return \App\Models\Branch model relation value. |
108
|
|
|
* |
109
|
|
|
* @return \App\Models\Branch |
110
|
|
|
*/ |
111
|
|
|
public function getBranchRelationValue(): Branch |
112
|
|
|
{ |
113
|
|
|
return $this->getRelationValue('branch'); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Set \App\Models\Branch model relation value. |
118
|
|
|
* |
119
|
|
|
* @param \App\Models\Branch $branch |
120
|
|
|
* @return $this |
121
|
|
|
*/ |
122
|
|
|
public function setBranchRelationValue(Branch $branch) |
123
|
|
|
{ |
124
|
|
|
$this->branch()->associate($branch); |
125
|
|
|
|
126
|
|
|
return $this; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Define a one-to-many relationship with App\Models\OrderStatus. |
131
|
|
|
* |
132
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany |
133
|
|
|
*/ |
134
|
3 |
|
public function statuses(): HasMany |
135
|
|
|
{ |
136
|
3 |
|
return $this->hasMany(OrderStatus::class); |
|
|
|
|
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Return collection of \App\Models\OrderStatus model relation value. |
141
|
|
|
* |
142
|
|
|
* @return \Illuminate\Database\Eloquent\Collection<\App\Models\OrderStatus> |
143
|
|
|
*/ |
144
|
|
|
public function getStatusesRelationValue(): Collection |
145
|
|
|
{ |
146
|
|
|
return $this->getCollectionValue('statuses', OrderStatus::class); |
|
|
|
|
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Set collection of \App\Models\OrderStatus model relation value. |
151
|
|
|
* |
152
|
|
|
* @param \Illuminate\Database\Eloquent\Collection<\App\Models\OrderStatus> $statuses |
153
|
|
|
* @return $this |
154
|
|
|
*/ |
155
|
|
|
public function setStatusesRelationValue(Collection $statuses) |
156
|
|
|
{ |
157
|
|
|
if ($this->isCollectionValid($statuses, OrderStatus::class)) { |
|
|
|
|
158
|
|
|
$this->setRelation('statuses', $statuses); |
|
|
|
|
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
return $this; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Define a one-to-one relationship with \App\Models\OrderStatus from a larger one-to-many relationship. |
166
|
|
|
* |
167
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasOne |
168
|
|
|
*/ |
169
|
|
|
public function latestStatus(): HasOne |
170
|
|
|
{ |
171
|
|
|
return $this->hasOne(OrderStatus::class)->latestOfMany('created_at'); |
|
|
|
|
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Return \App\Models\OrderStatus model relation value. |
176
|
|
|
* |
177
|
|
|
* @return \App\Models\OrderStatus |
178
|
|
|
*/ |
179
|
|
|
public function getLatestStatusRelationValue(): ?OrderStatus |
180
|
|
|
{ |
181
|
|
|
return $this->getRelationValue('latestStatus'); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Define a one-to-many relationship with App\Models\Item. |
186
|
|
|
* |
187
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany |
188
|
|
|
*/ |
189
|
3 |
|
public function items(): HasMany |
190
|
|
|
{ |
191
|
3 |
|
return $this->hasMany(Item::class); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Return collection of \App\Models\Item model relation value. |
196
|
|
|
* |
197
|
|
|
* @return \Illuminate\Database\Eloquent\Collection<\App\Models\Item> |
198
|
|
|
*/ |
199
|
|
|
public function getItemsRelationValue(): Collection |
200
|
|
|
{ |
201
|
|
|
return $this->getCollectionValue('items', Item::class); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* Set collection of \App\Models\Item model relation value. |
206
|
|
|
* |
207
|
|
|
* @param \Illuminate\Database\Eloquent\Collection<\App\Models\Item> $items |
208
|
|
|
* @return $this |
209
|
|
|
*/ |
210
|
|
|
public function setItemsRelationValue(Collection $items) |
211
|
|
|
{ |
212
|
|
|
if ($this->isCollectionValid($items, Item::class)) { |
213
|
|
|
$this->setRelation('items', $items); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
return $this; |
217
|
|
|
} |
218
|
|
|
} |
219
|
|
|
|