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: the controller needs phpunit testing. |
15
|
|
|
*/ |
16
|
|
|
class DepartmentController extends Controller |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* DepartmentController constructor. |
20
|
|
|
*/ |
21
|
|
|
public function __construct() |
22
|
|
|
{ |
23
|
|
|
$this->middleware('auth'); |
24
|
|
|
$this->middleware('lang'); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Display a listing of the resource. |
29
|
|
|
* |
30
|
|
|
* @return \Illuminate\Http\Response |
31
|
|
|
*/ |
32
|
|
|
public function index() |
33
|
|
|
{ |
34
|
|
|
$data['query'] = Departments::paginate(15); |
|
|
|
|
35
|
|
|
return view('departments.index', $data); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Show the form for creating a new resource. |
40
|
|
|
* |
41
|
|
|
* @return \Illuminate\Http\Response |
42
|
|
|
*/ |
43
|
|
|
public function create() |
44
|
|
|
{ |
45
|
|
|
return view('departments.insert'); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Store a newly created resource in storage. |
50
|
|
|
* |
51
|
|
|
* @param Requests\DepartmentsValidator $input |
52
|
|
|
* @return \Illuminate\Http\Response |
53
|
|
|
*/ |
54
|
|
|
public function store(Requests\DepartmentsValidator $input) |
55
|
|
|
{ |
56
|
|
|
Departments::create($input->except('_token')); |
57
|
|
|
return redirect()->route('departments.index'); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Display the specified resource. |
62
|
|
|
* |
63
|
|
|
* @param int $id the department id in the database. |
64
|
|
|
* @return \Illuminate\Http\Response |
65
|
|
|
*/ |
66
|
|
|
public function show($id) |
67
|
|
|
{ |
68
|
|
|
$data['query'] = Departments::find($id); |
|
|
|
|
69
|
|
|
return view('departments.specific', $data); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Show the form for editing the specified resource. |
74
|
|
|
* |
75
|
|
|
* @param int $id the department id in the database. |
76
|
|
|
* @return \Illuminate\Http\Response |
77
|
|
|
*/ |
78
|
|
|
public function edit($id) |
79
|
|
|
{ |
80
|
|
|
$data['query'] = Departments::find($id); |
|
|
|
|
81
|
|
|
return view('departments.update', $data); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Update the specified resource in storage. |
86
|
|
|
* |
87
|
|
|
* @param Requests\DepartmentsValidator $input |
88
|
|
|
* @param int $id the department id in the database. |
89
|
|
|
* @return \Illuminate\Http\Response |
90
|
|
|
*/ |
91
|
|
|
public function update(Requests\DepartmentsValidator $input, $id) |
92
|
|
|
{ |
93
|
|
|
Departments::find($id)->update($input->except('_token')); |
94
|
|
|
return redirect()->route('departments.index'); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Remove the specified resource from storage. |
99
|
|
|
* |
100
|
|
|
* @param int $id the department id in the database. |
101
|
|
|
* @return \Illuminate\Http\Response |
102
|
|
|
*/ |
103
|
|
|
public function destroy($id) |
104
|
|
|
{ |
105
|
|
|
Departments::destroy($id); |
106
|
|
|
return redirect()->back(302); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
View Code Duplication |
public function get_departments() |
|
|
|
|
110
|
|
|
{ |
111
|
|
|
$items = Departments::all(); |
112
|
|
|
$data2 = []; |
113
|
|
|
foreach($items as $department) |
114
|
|
|
{ |
115
|
|
|
$data2[] = [ |
116
|
|
|
'value' => $department["id"], |
117
|
|
|
'text' => $department["name"] |
118
|
|
|
]; |
119
|
|
|
|
120
|
|
|
} |
121
|
|
|
return json_encode($data2); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
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.