1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* App\Warehouse |
9
|
|
|
* |
10
|
|
|
* @property integer $id |
11
|
|
|
* @property string $name |
12
|
|
|
* @property \Carbon\Carbon $deleted_at |
13
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Rack[] $racks |
14
|
|
|
* @method static \Illuminate\Database\Query\Builder|\App\Warehouse whereId($value) |
15
|
|
|
* @method static \Illuminate\Database\Query\Builder|\App\Warehouse whereName($value) |
16
|
|
|
* @method static \Illuminate\Database\Query\Builder|\App\Warehouse whereDeletedAt($value) |
17
|
|
|
* @mixin \Eloquent |
18
|
|
|
*/ |
19
|
|
|
class Warehouse extends Eloquent\Model |
20
|
|
|
{ |
21
|
|
|
use Eloquent\SoftDeletes; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* We do not want any timestamps. |
25
|
|
|
* |
26
|
|
|
* @var bool |
27
|
|
|
*/ |
28
|
|
|
public $timestamps = false; |
29
|
|
|
/** |
30
|
|
|
* @var array |
31
|
|
|
*/ |
32
|
|
|
protected $dates = ['deleted_at']; |
33
|
|
|
/** |
34
|
|
|
* The attributes that are mass assignable. |
35
|
|
|
* |
36
|
|
|
* @var array |
37
|
|
|
*/ |
38
|
|
|
protected $fillable = ['name']; |
39
|
|
|
|
40
|
11 |
|
public static function boot() |
41
|
|
|
{ |
42
|
11 |
|
parent::boot(); |
43
|
|
|
|
44
|
11 |
|
static::deleting(function (Warehouse $warehouse) { |
45
|
|
|
$warehouse->racks()->delete(); |
46
|
11 |
|
}); |
47
|
11 |
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Guitars in the warehouse. |
51
|
|
|
* |
52
|
|
|
* @return Eloquent\Relations\HasManyThrough |
53
|
|
|
*/ |
54
|
|
|
public function guitars() |
55
|
|
|
{ |
56
|
|
|
return $this->hasManyThrough(Guitar::class, Rack::class); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Add racks to the warehouse. |
61
|
|
|
* |
62
|
|
|
* @param Eloquent\Collection|array $racks |
63
|
|
|
* |
64
|
|
|
* @return $this |
65
|
|
|
*/ |
66
|
2 |
|
public function addRacks($racks) |
67
|
|
|
{ |
68
|
2 |
|
foreach ($racks as $rack) { |
69
|
2 |
|
$this->addRack($rack); |
70
|
|
|
} |
71
|
|
|
|
72
|
2 |
|
return $this; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Add rack to the warehouse. |
77
|
|
|
* |
78
|
|
|
* @param array|Rack $rack |
79
|
|
|
* |
80
|
|
|
* @return Eloquent\Model |
81
|
|
|
*/ |
82
|
4 |
|
public function addRack($rack) |
83
|
|
|
{ |
84
|
4 |
|
if (is_array($rack)) { |
85
|
1 |
|
$rack = new Rack($rack); |
86
|
|
|
} |
87
|
4 |
|
$this->racks()->save($rack); |
88
|
|
|
|
89
|
4 |
|
return $this; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Racks in the warehouse. |
94
|
|
|
* |
95
|
|
|
* @return Eloquent\Relations\HasMany |
96
|
|
|
*/ |
97
|
6 |
|
public function racks() |
98
|
|
|
{ |
99
|
6 |
|
return $this->hasMany(Rack::class); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Remove racks using ids. |
104
|
|
|
* |
105
|
|
|
* @param $ids |
106
|
|
|
* |
107
|
|
|
* @return $this |
108
|
|
|
*/ |
109
|
1 |
|
public function removeRacksNotIn($ids) |
110
|
|
|
{ |
111
|
1 |
|
$this->racks()->whereNotIn('id', $ids)->delete(); |
112
|
|
|
|
113
|
1 |
|
return $this; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Add or update the racks. |
118
|
|
|
* |
119
|
|
|
* @param $racks |
120
|
|
|
* |
121
|
|
|
* @return $this |
122
|
|
|
*/ |
123
|
|
|
public function addOrUpdateRacks($racks) |
124
|
|
|
{ |
125
|
|
|
foreach ($racks as $rack) { |
126
|
|
|
$this->addOrUpdateRack($rack); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
return $this; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Add or Update Racks. |
134
|
|
|
* |
135
|
|
|
* @param $rack |
136
|
|
|
* |
137
|
|
|
* @return $this |
138
|
|
|
*/ |
139
|
2 |
|
public function addOrUpdateRack($rack) |
140
|
|
|
{ |
141
|
2 |
|
(array_has($rack, 'id')) ? |
142
|
2 |
|
$this->racks()->find($rack['id'])->update($rack) : $this->addRack($rack); |
143
|
|
|
|
144
|
2 |
|
return $this; |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|