BillPayController::toggle()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 1
dl 0
loc 10
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\Category;
6
use Illuminate\Http\Request;
7
use Session;
8
9
class BillPayController extends Controller
10
{
11
    /**
12
     * Create a new controller instance.
13
     *
14
     * @return void
15
     */
16
    public function __construct()
17
    {
18
        $this->middleware('auth');
19
    }
20
    
21
    /**
22
     * Display a listing of the resource.
23
     *
24
     * @return \Illuminate\Http\Response
25
     */
26
    public function index()
27
    {
28
        $bill_pays = auth()->user()->bill_pays()->orderBy('name', 'asc')->paginate(10);
29
        return view('bill_pays.index', compact('bill_pays'));
0 ignored issues
show
Bug Best Practice introduced by
The expression return view('bill_pays.i..., compact('bill_pays')) returns the type Illuminate\View\View which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
30
    }
31
32
    /**
33
     * Show the form for creating a new resource.
34
     *
35
     * @return \Illuminate\Http\Response
36
     */
37
    public function create()
38
    {
39
        $categories = auth()->user()->categories()->get();
40
        return view('bill_pays.create', compact('categories'));
0 ignored issues
show
Bug Best Practice introduced by
The expression return view('bill_pays.c... compact('categories')) returns the type Illuminate\View\View which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
41
    }
42
43
    /**
44
     * Store a newly created resource in storage.
45
     *
46
     * @param  \Illuminate\Http\Request  $request
47
     * @return \Illuminate\Http\Response
48
     */
49
    public function store(Request $request)
50
    {
51
        $this->validate($request, [
52
            'category_id' => 'required',
53
            'date_launch' => 'required|date',
54
            'name' => 'required|min:3|max:255',
55
            'value' => 'required|numeric'
56
        ]);
57
        
58
        auth()->user()->bill_pays()->create($request->all());
59
60
        Session::flash('success', 'Conta a pagar criada com sucesso');
61
62
        return redirect()->route('bill_pays.index');
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect()->route('bill_pays.index') returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
63
    }
64
65
    /**
66
     * Show the form for editing the specified resource.
67
     *
68
     * @param  int  $id
69
     * @return \Illuminate\Http\Response
70
     */
71
    public function edit($id)
72
    {
73
        $categories = auth()->user()->categories()->get();
74
        $bill_pay = auth()->user()->bill_pays()->findOrFail($id);
75
        return view('bill_pays.edit', compact('bill_pay','categories'));
0 ignored issues
show
Bug Best Practice introduced by
The expression return view('bill_pays.e...ll_pay', 'categories')) returns the type Illuminate\View\View which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
76
    }
77
78
    /**
79
     * Update the specified resource in storage.
80
     *
81
     * @param  \Illuminate\Http\Request  $request
82
     * @param  int  $id
83
     * @return \Illuminate\Http\Response
84
     */
85
    public function update(Request $request, $id)
86
    {
87
        $this->validate($request, [
88
            'category_id' => 'required',
89
            'date_launch' => 'required|date',
90
            'name' => 'required|min:3|max:255',
91
            'value' => 'required|numeric'
92
        ]);
93
94
        $bill_pay = auth()->user()->bill_pays()->findOrFail($id);
95
        $bill_pay->update($request->all());
96
                
97
        Session::flash('success', 'Conta a pagar atualizada com sucesso');
98
99
        return redirect()->route('bill_pays.index');
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect()->route('bill_pays.index') returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
100
    }
101
102
    /**
103
     * Remove the specified resource from storage.
104
     *
105
     * @param  int  $id
106
     * @return \Illuminate\Http\Response
107
     */
108
    public function destroy($id)
109
    {
110
        $bill_pay = auth()->user()->bill_pays()->findOrFail($id);
111
        $bill_pay->delete();
112
113
        Session::flash('success', 'Conta a pagar deletada com sucesso');
114
115
        return redirect()->back();
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect()->back() returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
116
    }
117
118
    /**
119
     * Update the specified resource in storage.
120
     *
121
     * @param  \Illuminate\Http\Request  $request
122
     * @return \Illuminate\Http\Response
123
     */
124
    public function toggle(Request $request)
125
    {
126
        $this->validate($request, [
127
            "id" => "exists:bill_pays,id"
128
        ]);
129
130
        $bill_pay = auth()->user()->bill_pays()->findOrFail($request->id);
131
        $bill_pay->update(['status' => !$bill_pay->status]);
132
                
133
        return response()->json(['status' => 'success', 'message' => 'Conta a pagar atualizada com sucesso']);
0 ignored issues
show
Bug Best Practice introduced by
The expression return response()->json(...ualizada com sucesso')) returns the type Illuminate\Http\JsonResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
134
    }
135
}
136