1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Http\Controllers; |
4
|
|
|
|
5
|
|
|
use App\Events\GuitarWasCreated; |
6
|
|
|
use App\Events\GuitarWasDeleted; |
7
|
|
|
use App\Events\GuitarWasUpdated; |
8
|
|
|
use App\Guitar; |
9
|
|
|
use App\Purchase; |
10
|
|
|
use App\Rack; |
11
|
|
|
use Illuminate\Http\Request; |
12
|
|
|
use Illuminate\Http\Response; |
13
|
|
|
|
14
|
|
|
class GuitarController extends Controller |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* GuitarController constructor. |
18
|
|
|
*/ |
19
|
|
|
public function __construct() |
20
|
|
|
{ |
21
|
|
|
$this->middleware('auth'); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* List the guitars. |
26
|
|
|
* @return Response |
27
|
|
|
*/ |
28
|
|
|
public function index() |
29
|
|
|
{ |
30
|
|
|
return view('guitars.index'); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Save the guitar. |
35
|
|
|
* |
36
|
|
|
* @param $id |
37
|
|
|
* @param Request $request |
38
|
|
|
* |
39
|
|
|
* @return Response |
40
|
|
|
*/ |
41
|
|
|
public function store($id, Request $request) |
42
|
|
|
{ |
43
|
|
|
$purchase = Purchase::findOrFail($id); |
44
|
|
|
if ( ! $purchase->hasArrived() || ! $purchase->isPendingStorage()) { |
45
|
|
|
return response(['purchase' => ['The purchase has not yet arrived or doesn\'t require addition of guitars.']], |
|
|
|
|
46
|
|
|
422); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
$this->validate($request, [ |
50
|
|
|
'rack_id' => 'required|exists:racks,id,make_id,' . $purchase->make_id, |
51
|
|
|
'model_id' => 'required|exists:models,id,make_id,' . $purchase->make_id, |
52
|
|
|
'colour' => 'required|string|max:255', |
53
|
|
|
'damaged' => 'boolean', |
54
|
|
|
'condition' => 'required_if:damaged,true', |
55
|
|
|
'price' => 'required|numeric|min:1', |
56
|
|
|
]); |
57
|
|
|
|
58
|
|
|
$rack = Rack::find($request->input('rack_id')); |
59
|
|
View Code Duplication |
if ($rack->used >= $rack->capacity) { |
|
|
|
|
60
|
|
|
return response(['rack_id' => ['The selected rack is full.']], 422); |
|
|
|
|
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$guitar = Guitar::create($request->only(['rack_id', 'model_id', 'colour', 'price']) + [ |
64
|
|
|
'make_id' => $purchase->make_id, |
65
|
|
|
'purchase_id' => $purchase->id, |
66
|
|
|
'damaged' => $request->input('damaged'), |
67
|
|
|
'condition' => $request->input('damaged') ? $request->input('condition') : null |
68
|
|
|
]); |
69
|
|
|
|
70
|
|
|
event(new GuitarWasCreated($guitar)); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Update the guitar. |
75
|
|
|
* |
76
|
|
|
* @param $id |
77
|
|
|
* @param Request $request |
78
|
|
|
* |
79
|
|
|
* @return Response |
80
|
|
|
*/ |
81
|
|
|
public function update($id, Request $request) |
82
|
|
|
{ |
83
|
|
|
$guitar = Guitar::findOrFail($id); |
84
|
|
|
$this->validate($request, [ |
85
|
|
|
'rack_id' => 'required|exists:racks,id,make_id,' . $guitar->make_id, |
86
|
|
|
'model_id' => 'required|exists:models,id,make_id,' . $guitar->make_id, |
87
|
|
|
'colour' => 'required|string|max:255', |
88
|
|
|
'damaged' => 'boolean', |
89
|
|
|
'condition' => 'required_if:damaged,true', |
90
|
|
|
'price' => 'required|numeric|min:1', |
91
|
|
|
]); |
92
|
|
|
|
93
|
|
|
$rack = Rack::find($request->input('rack_id')); |
94
|
|
View Code Duplication |
if ($rack->used >= $rack->capacity) { |
|
|
|
|
95
|
|
|
return response(['rack_id' => ['The selected rack is full.']], 422); |
|
|
|
|
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
$guitar = $guitar->update($request->only(['rack_id', 'model_id', 'colour', 'price']) + [ |
99
|
|
|
'damaged' => $request->input('damaged'), |
100
|
|
|
'condition' => $request->input('damaged') ? $request->input('condition') : null |
101
|
|
|
]); |
102
|
|
|
event(new GuitarWasUpdated($guitar)); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Delete the guitar. |
107
|
|
|
* |
108
|
|
|
* @param $id |
109
|
|
|
*/ |
110
|
|
|
public function destroy($id) |
111
|
|
|
{ |
112
|
|
|
$guitar = Guitar::findOrFail($id); |
113
|
|
|
event(new GuitarWasDeleted($guitar)); |
114
|
|
|
$guitar->delete(); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: