1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Http\Controllers; |
4
|
|
|
|
5
|
|
|
use App\Departments; |
6
|
|
|
use Illuminate\Http\Request; |
7
|
|
|
|
8
|
|
|
use App\Http\Requests; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class DepartmentController |
12
|
|
|
* @package App\Http\Controllers |
13
|
|
|
* |
14
|
|
|
* TODO: set routes. |
15
|
|
|
* TODO: the controller needs phpunit testing. |
16
|
|
|
*/ |
17
|
|
|
class DepartmentController extends Controller |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* DepartmentController constructor. |
21
|
|
|
*/ |
22
|
|
|
public function __construct() |
23
|
|
|
{ |
24
|
|
|
$this->middleware('auth'); |
25
|
|
|
$this->middleware('lang'); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Display a listing of the resource. |
30
|
|
|
* |
31
|
|
|
* @return \Illuminate\Http\Response |
32
|
|
|
*/ |
33
|
|
|
public function index() |
34
|
|
|
{ |
35
|
|
|
$data['query'] = Departments::with('managers')->paginate(15); |
|
|
|
|
36
|
|
|
return view('departments.index', $data); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Show the form for creating a new resource. |
41
|
|
|
* |
42
|
|
|
* @return \Illuminate\Http\Response |
43
|
|
|
*/ |
44
|
|
|
public function create() |
45
|
|
|
{ |
46
|
|
|
return view('departments.insert'); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Search for a specific department |
51
|
|
|
* |
52
|
|
|
* @param Request $request |
53
|
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
54
|
|
|
*/ |
55
|
|
|
public function search(Request $request) |
56
|
|
|
{ |
57
|
|
|
$term = $request->get('name'); |
58
|
|
|
$data['query'] = Departments::where('name', 'LIKE', "%$term%")->with('managers')->paginate(15); |
|
|
|
|
59
|
|
|
return view('departments.index', $data); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Store a newly created resource in storage. |
64
|
|
|
* |
65
|
|
|
* @param Requests\DepartmentsValidator $input |
66
|
|
|
* @return \Illuminate\Http\Response |
67
|
|
|
*/ |
68
|
|
|
public function store(Requests\DepartmentsValidator $input) |
69
|
|
|
{ |
70
|
|
|
Departments::create($input->except('_token')); |
71
|
|
|
return redirect()->route('departments.index'); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Display the specified resource. |
76
|
|
|
* |
77
|
|
|
* @param int $id the department id in the database. |
78
|
|
|
* @return \Illuminate\Http\Response |
79
|
|
|
*/ |
80
|
|
|
public function show($id) |
81
|
|
|
{ |
82
|
|
|
$data['query'] = Departments::find($id); |
|
|
|
|
83
|
|
|
return view('departments.specific', $data); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Show the form for editing the specified resource. |
88
|
|
|
* |
89
|
|
|
* @param int $id the department id in the database. |
90
|
|
|
* @return \Illuminate\Http\Response |
91
|
|
|
*/ |
92
|
|
|
public function edit($id) |
93
|
|
|
{ |
94
|
|
|
$data['query'] = Departments::find($id); |
|
|
|
|
95
|
|
|
return view('departments.update', $data); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Update the specified resource in storage. |
100
|
|
|
* |
101
|
|
|
* @param Requests\DepartmentsValidator $input |
102
|
|
|
* @param int $id the department id in the database. |
103
|
|
|
* @return \Illuminate\Http\Response |
104
|
|
|
*/ |
105
|
|
|
public function update(Requests\DepartmentsValidator $input, $id) |
106
|
|
|
{ |
107
|
|
|
Departments::find($id)->update($input->except('_token')); |
108
|
|
|
return redirect()->route('departments.index'); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Remove the specified resource from storage. |
113
|
|
|
* |
114
|
|
|
* @param int $id the department id in the database. |
115
|
|
|
* @return \Illuminate\Http\Response |
116
|
|
|
*/ |
117
|
|
|
public function destroy($id) |
118
|
|
|
{ |
119
|
|
|
Departments::destroy($id); |
120
|
|
|
return redirect()->back(302); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Get all the departments |
125
|
|
|
* |
126
|
|
|
* @return string |
127
|
|
|
*/ |
128
|
|
View Code Duplication |
public function get_departments() |
|
|
|
|
129
|
|
|
{ |
130
|
|
|
$items = Departments::all(); |
131
|
|
|
$data2 = []; |
132
|
|
|
|
133
|
|
|
foreach($items as $department) { |
134
|
|
|
$data2[] = [ |
135
|
|
|
'value' => $department["id"], |
136
|
|
|
'text' => $department["name"] |
137
|
|
|
]; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
return json_encode($data2); |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.