1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Http\Controllers; |
4
|
|
|
|
5
|
|
|
use App\User; |
6
|
|
|
use Illuminate\Http\Request; |
7
|
|
|
|
8
|
|
|
use App\Http\Requests; |
9
|
|
|
use Bouncer; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class StaffController |
13
|
|
|
* @package App\Http\Controllers |
14
|
|
|
*/ |
15
|
|
|
class StaffController extends Controller |
16
|
|
|
{ |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* StaffController constructor. |
20
|
|
|
*/ |
21
|
|
|
public function __construct() |
22
|
|
|
{ |
23
|
|
|
$this->middleware('auth'); |
24
|
|
|
$this->middleware('lang'); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Create new staff member. |
29
|
|
|
* |
30
|
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
31
|
|
|
*/ |
32
|
|
|
public function create() |
33
|
|
|
{ |
34
|
|
|
return view('staff.create'); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Edit view for a staff member. |
39
|
|
|
* |
40
|
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
41
|
|
|
*/ |
42
|
|
|
public function edit($id) |
43
|
|
|
{ |
44
|
|
|
$data['query'] = User::find($id); |
|
|
|
|
45
|
|
|
return view('staff.edit', $data); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Display all the staff. |
50
|
|
|
* |
51
|
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
52
|
|
|
*/ |
53
|
|
|
public function index() |
54
|
|
|
{ |
55
|
|
|
$data['users'] = User::paginate(15); |
|
|
|
|
56
|
|
|
return view('users/index', $data); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Set the user available. |
61
|
|
|
* |
62
|
|
|
* @return \Illuminate\Http\RedirectResponse |
63
|
|
|
*/ |
64
|
|
View Code Duplication |
public function setAvailable() |
|
|
|
|
65
|
|
|
{ |
66
|
|
|
$user = User::find(auth()->user()->id); |
|
|
|
|
67
|
|
|
Bouncer::retract('unavailable')->from($user); |
68
|
|
|
Bouncer::assign('available')->to($user); |
69
|
|
|
|
70
|
|
|
return redirect()->back(302); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Set the user unavailable. |
75
|
|
|
* |
76
|
|
|
* @return \Illuminate\Http\RedirectResponse |
77
|
|
|
*/ |
78
|
|
View Code Duplication |
public function setUnavailable() |
|
|
|
|
79
|
|
|
{ |
80
|
|
|
$user = User::find(auth()->user()->id); |
|
|
|
|
81
|
|
|
Bouncer::retract('available')->from($user); |
82
|
|
|
Bouncer::assign('unavailable')->to($user); |
83
|
|
|
|
84
|
|
|
return redirect()->back(302); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Display the profile. |
89
|
|
|
* |
90
|
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
91
|
|
|
*/ |
92
|
|
|
public function profile() |
93
|
|
|
{ |
94
|
|
|
return view('users/profile'); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Destroy or multiple staff members. |
99
|
|
|
* |
100
|
|
|
* @param int $id THe id off the staff member in the database. |
101
|
|
|
* @return \Illuminate\Http\RedirectResponse |
102
|
|
|
*/ |
103
|
|
|
public function destroy($id) |
104
|
|
|
{ |
105
|
|
|
$user = User::find($id); |
106
|
|
|
$user->roles()->sync([]); |
107
|
|
|
|
108
|
|
|
User::destroy($id); |
109
|
|
|
session()->flash('message', 'User deleted'); |
110
|
|
|
|
111
|
|
|
return redirect()->to('/staff'); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
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.