Test Setup Failed
Push — dev6 ( 81193f...01d104 )
by Ron
22:34
created

CustomerEquipmentController::update()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 5
c 2
b 0
f 0
dl 0
loc 11
rs 10
cc 2
nc 2
nop 2
1
<?php
2
3
namespace App\Http\Controllers\Customers;
4
5
use App\Models\DataField;
6
use App\Models\CustomerEquipment;
7
use App\Http\Controllers\Controller;
8
use App\Models\CustomerEquipmentData;
9
use App\Http\Requests\Customers\CustomerEquipmentRequest;
10
11
use Illuminate\Support\Facades\Log;
12
13
class CustomerEquipmentController extends Controller
14
{
15
    /**
16
     *  Store the new Customer Equipment
17
     */
18
    public function store(CustomerEquipmentRequest $request)
19
    {
20
        //  Input the equipment type
21
        $newEquip = CustomerEquipment::create($request->only(['cust_id', 'equip_id', 'shared']));
22
23
        //  Input the equipment data
24
        foreach($request->data as $field)
25
        {
26
            CustomerEquipmentData::create([
27
                'cust_equip_id' => $newEquip->cust_equip_id,
28
                'field_id'      => DataField::where('equip_id', $request->equip_id)->where('type_id', $field['type_id'])->first()->field_id,//  ???
29
                'value'         => isset($field['value']) ? $field['value'] : null,
30
            ]);
31
        }
32
33
        Log::notice('New Equipment ID '.$request->equip_id.' has been added for Customer ID '.$request->cust_id);
34
        return back()->with(['message' => 'Successfully Added Equipment', 'type' => 'success']);
35
    }
36
37
    /**
38
     *  Ajax call to get the list of customer equipment
39
     */
40
    public function show($id)
41
    {
42
        return CustomerEquipment::with('CustomerEquipmentData')->where('cust_id', $id)->get();
43
    }
44
45
    /**
46
     *  Update the customers equipment information
47
     */
48
    public function update(CustomerEquipmentRequest $request, $id)
49
    {
50
        foreach($request->data as $field)
51
        {
52
            CustomerEquipmentData::where('id', $field['id'])->where('cust_equip_id', $id)->update([
53
                'value' => $field['value'],
54
            ]);
55
        }
56
57
        Log::notice('Customer Equipment ID '.$id.' has been updated for Customer ID '.$request->cust_id);
58
        return back()->with(['message' => 'Successfully Updated Equipment', 'type' => 'success']);
59
    }
60
61
    /**
62
     *  Remove the selected equipment
63
     */
64
    public function destroy($id)
65
    {
66
        CustomerEquipment::find($id)->delete();
67
        Log::notice('Equipment ID '.$id.' for a customer was deleted');
68
69
        return response()->noContent();
70
    }
71
}
72