1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Http\Controllers; |
4
|
|
|
|
5
|
|
|
use App\Events\GuitarWasCreated; |
6
|
|
|
use App\Events\WarehouseWasUpdated; |
7
|
|
|
use App\Guitar; |
8
|
|
|
use App\Purchase; |
9
|
|
|
use App\Rack; |
10
|
|
|
use Illuminate\Http\Request; |
11
|
|
|
|
12
|
|
|
class GuitarController extends Controller |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Save the guitar. |
16
|
|
|
* |
17
|
|
|
* @param $id |
18
|
|
|
*/ |
19
|
|
|
public function store($id, Request $request) |
20
|
|
|
{ |
21
|
|
|
$purchase = Purchase::findOrFail($id); |
22
|
|
|
if ( ! $purchase->hasArrived() || ! $purchase->isPendingStorage()) { |
23
|
|
|
return response(['purchase' => ['The purchase has not yet arrived or doesn\'t require addition of guitars.']], |
|
|
|
|
24
|
|
|
422); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
$this->validate($request, [ |
28
|
|
|
'rack_id' => 'required|exists:racks,id,make_id,' . $purchase->make_id, |
29
|
|
|
'model_id' => 'required|exists:models,id,make_id,' . $purchase->make_id, |
30
|
|
|
'colour' => 'required|string|max:255', |
31
|
|
|
'damaged' => 'boolean', |
32
|
|
|
'condition' => 'required_if:damaged,true', |
33
|
|
|
'price' => 'required|numeric|min:1', |
34
|
|
|
]); |
35
|
|
|
|
36
|
|
|
$rack = Rack::find($request->input('rack_id')); |
37
|
|
|
if ($rack->used >= $rack->capacity) { |
38
|
|
|
return response(['rack_id' => ['The selected rack is full.']], 422); |
|
|
|
|
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
$guitar = Guitar::create($request->only(['rack_id', 'model_id', 'colour', 'price']) + [ |
42
|
|
|
'make_id' => $purchase->make_id, |
43
|
|
|
'purchase_id' => $purchase->id, |
44
|
|
|
'damaged' => $request->input('damaged'), |
45
|
|
|
'condition' => $request->input('damaged') ? $request->input('condition') : null |
46
|
|
|
]); |
47
|
|
|
|
48
|
|
|
event(new GuitarWasCreated($guitar)); |
49
|
|
|
event(new WarehouseWasUpdated($rack->warehouse)); |
|
|
|
|
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
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: