Issues (465)

app/Http/Controllers/FamilyController.php (8 issues)

1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\Models\Family;
6
use App\Models\Person;
7
use DB;
8
use Illuminate\Http\Request;
9
10
class FamilyController extends Controller
11
{
12
    /**
13
     * Display a listing of the resource.
14
     *
15
     * @return \Illuminate\Http\Response
16
     */
17
    public function index(Request $request)
18
    {
19
        $query = Family::query()->with(['husband', 'wife']);
20
21
        if ($request->has('searchTerm')) {
22
            $columnsToSearch = ['description', 'is_active', 'husband_id', 'wife_id', 'type_id', 'chan', 'nchi', 'rin'];
23
            $search_term = json_decode($request->searchTerm)->searchTerm;
24
            if (! empty($search_term)) {
25
                $searchQuery = '%'.$search_term.'%';
26
                foreach ($columnsToSearch as $column) {
27
                    $query->orWhere($column, 'LIKE', $searchQuery);
28
                }
29
            }
30
        }
31
32
        if ($request->has('columnFilters')) {
33
            $filters = get_object_vars(json_decode($request->columnFilters));
34
35
            foreach ($filters as $key => $value) {
36
                if (! empty($value)) {
37
                    $query->orWhere($key, 'like', '%'.$value.'%');
38
                }
39
            }
40
        }
41
42
        if ($request->has('sort.0')) {
43
            $sort = json_decode($request->sort[0]);
44
            $query->orderBy($sort->field, $sort->type);
45
        }
46
47
        if ($request->has('perPage')) {
48
            $rows = $query->paginate($request->perPage);
49
        }
50
        if (! count($request->all())) {
51
            $rows = $query->get();
52
        }
53
54
        return $rows;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $rows does not seem to be defined for all execution paths leading up to this point.
Loading history...
55
    }
56
57
    /**
58
     * Show the form for creating a new resource.
59
     *
60
     * @return \Illuminate\Http\Response
61
     */
62
    public function create()
63
    {
64
        $male = Person::where('sex', 'M')->get();
65
        $female = Person::where('sex', 'F')->get();
66
        $types = DB::table('types')->get();
67
68
        return response()->json(['male' => $male, 'female' => $female, 'types' => $types]);
0 ignored issues
show
Bug Best Practice introduced by
The expression return response()->json(...le, 'types' => $types)) returns the type Illuminate\Http\JsonResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
69
    }
70
71
    /**
72
     * Store a newly created resource in storage.
73
     *
74
     * @param  \Illuminate\Http\Request  $request
75
     * @return \Illuminate\Http\Response
76
     */
77
    public function store(Request $request)
78
    {
79
        $request->validate([
80
            'description' => 'required',
81
            'is_active' => 'required',
82
            'husband_id' => 'required',
83
            'wife_id' => 'required',
84
            'type_id' => 'required',
85
            'chan' => 'required',
86
            'nchi' => 'required',
87
            'rin' => 'required',
88
        ]);
89
90
        return Family::create([
0 ignored issues
show
Bug Best Practice introduced by
The expression return App\Models\Family...rin' => $request->rin)) also could return the type App\Models\Family which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
91
            'description' => $request->description,
92
            'is_active' => $request->is_active,
93
            'husband_id' => $request->husband_id,
94
            'wife_id' => $request->wife_id,
95
            'type_id' => $request->type_id,
96
            'chan' => $request->chan,
97
            'nchi' => $request->nchi,
98
            'rin' => $request->rin,
99
        ]);
100
    }
101
102
    /**
103
     * Display the specified resource.
104
     *
105
     * @param  int  $id
106
     * @return \Illuminate\Http\Response
107
     */
108
    public function show($id)
109
    {
110
        return Family::find($id);
0 ignored issues
show
Bug Best Practice introduced by
The expression return App\Models\Family::find($id) also could return the type App\Models\Family which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
111
    }
112
113
    /**
114
     * Show the form for editing the specified resource.
115
     *
116
     * @param  int  $id
117
     * @return \Illuminate\Http\Response
118
     */
119
    public function edit($id)
0 ignored issues
show
The parameter $id is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

119
    public function edit(/** @scrutinizer ignore-unused */ $id)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
120
    {
121
        //
122
    }
123
124
    /**
125
     * Update the specified resource in storage.
126
     *
127
     * @param  \Illuminate\Http\Request  $request
128
     * @param  int  $id
129
     * @return \Illuminate\Http\Response
130
     */
131
    public function update(Request $request, $id)
132
    {
133
        $request->validate([
134
            'description' => 'required',
135
            'is_active' => 'required',
136
            'husband_id' => 'required',
137
            'wife_id' => 'required',
138
            'type_id' => 'required',
139
            'chan' => 'required',
140
            'nchi' => 'required',
141
            'rin' => 'required',
142
        ]);
143
144
        $family = Family::find($id);
145
        $family->description = $request->description;
146
        $family->is_active = $request->is_active;
147
        $family->husband_id = $request->husband_id;
148
        $family->wife_id = $request->wife_id;
149
        $family->type_id = $request->type_id;
150
        $family->chan = $request->chan;
151
        $family->nchi = $request->nchi;
152
        $family->rin = $request->rin;
153
        $family->save();
154
155
        return $family;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $family also could return the type App\Models\Family which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
156
    }
157
158
    /**
159
     * Remove the specified resource from storage.
160
     *
161
     * @param  int  $id
162
     * @return \Illuminate\Http\Response
163
     */
164
    public function destroy($id)
165
    {
166
        $family = Family::find($id);
167
        if ($family) {
168
            $family->delete();
169
170
            return 'true';
0 ignored issues
show
Bug Best Practice introduced by
The expression return 'true' returns the type string which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
171
        }
172
173
        return 'false';
0 ignored issues
show
Bug Best Practice introduced by
The expression return 'false' returns the type string which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
174
    }
175
}
176