Issues (465)

app/Http/Controllers/CompanyController.php (6 issues)

1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\Models\Company;
6
use Illuminate\Http\Request;
7
8
class CompanyController extends Controller
9
{
10
    /**
11
     * Display a listing of the resource.
12
     *
13
     * @return \Illuminate\Http\Response
14
     */
15
    public function index(Request $request)
16
    {
17
        $user = auth()->user();
18
19
        $companies_id = $user->Company()->pluck('companies.id');
20
        $query = Company::query();
21
22
        if ($request->has('searchTerm')) {
23
            $columnsToSearch = ['name', 'status'];
24
            $search_term = json_decode($request->searchTerm)->searchTerm;
25
            if (! empty($search_term)) {
26
                $searchQuery = '%'.$search_term.'%';
27
                foreach ($columnsToSearch as $column) {
28
                    $query->orWhere($column, 'LIKE', $searchQuery);
29
                }
30
            }
31
        }
32
33
        if ($request->has('columnFilters')) {
34
            $filters = get_object_vars(json_decode($request->columnFilters));
35
36
            foreach ($filters as $key => $value) {
37
                if (! empty($value)) {
38
                    $query->orWhere($key, 'like', '%'.$value.'%');
39
                }
40
            }
41
        }
42
43
        if ($request->has('sort.0')) {
44
            $sort = json_decode($request->sort[0]);
45
            $query->orderBy($sort->field, $sort->type);
46
        }
47
        $query->find($companies_id);
48
        if ($request->has('perPage')) {
49
            $rows = $query->paginate($request->perPage);
50
        }
51
        if (! count($request->all())) {
52
            $rows = $query->get()->toArray();
53
        }
54
55
        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...
56
    }
57
58
    /**
59
     * Show the form for creating a new resource.
60
     *
61
     * @return \Illuminate\Http\Response
62
     */
63
    public function create()
64
    {
65
        //
66
    }
67
68
    /**
69
     * Store a newly created resource in storage.
70
     *
71
     * @param  \Illuminate\Http\Request  $request
72
     * @return \Illuminate\Http\Response
73
     */
74
    public function store(Request $request)
75
    {
76
        $user = auth()->user();
77
78
        //
79
        $request->validate([
80
            'name' => ['required', 'unique:landlord.companies'],
81
82
        ]);
83
84
        return $user->Company()->create([
85
            'name' => $request->name,
86
            'status' => 1,
87
            'current_tenant' => 0,
88
        ]);
89
    }
90
91
    /**
92
     * Display the specified resource.
93
     *
94
     * @param  int  $id
95
     * @return \Illuminate\Http\Response
96
     */
97
    public function show($id)
98
    {
99
        //
100
        return Company::find($id);
0 ignored issues
show
Bug Best Practice introduced by
The expression return App\Models\Company::find($id) also could return the type App\Models\Company which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
101
    }
102
103
    /**
104
     * Show the form for editing the specified resource.
105
     *
106
     * @param  int  $id
107
     * @return \Illuminate\Http\Response
108
     */
109
    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

109
    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...
110
    {
111
        //
112
    }
113
114
    /**
115
     * Update the specified resource in storage.
116
     *
117
     * @param  \Illuminate\Http\Request  $request
118
     * @param  int  $id
119
     * @return \Illuminate\Http\Response
120
     */
121
    public function update(Request $request, $id)
122
    {
123
        //
124
        $request->validate([
125
            'name' =>  ['required', 'unique:landlord.companies'],
126
127
        ]);
128
129
        $company = Company::find($id);
130
        $company->name = $request->name;
131
        $company->save();
132
133
        return $company;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $company also could return the type App\Models\Company which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
134
    }
135
136
    /**
137
     * Remove the specified resource from storage.
138
     *
139
     * @param  int  $id
140
     * @return \Illuminate\Http\Response
141
     */
142
    public function destroy($id)
143
    {
144
        //
145
        $company = Company::find($id);
146
        if ($company) {
147
            $company->delete();
148
149
            return true;
0 ignored issues
show
Bug Best Practice introduced by
The expression return true returns the type true which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
150
        }
151
152
        return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
153
    }
154
}
155