|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace Sco\Admin\Http\Controllers\Manager; |
|
5
|
|
|
|
|
6
|
|
|
use Illuminate\Http\Request; |
|
7
|
|
|
use Illuminate\Routing\Controller; |
|
8
|
|
|
use Sco\Admin\Exceptions\AdminHttpException; |
|
9
|
|
|
use Sco\Admin\Http\Requests\ManagerRequest; |
|
10
|
|
|
use Sco\Admin\Models\Manager; |
|
11
|
|
|
use Sco\Admin\Models\Role; |
|
12
|
|
|
|
|
13
|
|
|
class UserController extends Controller |
|
14
|
|
|
{ |
|
15
|
|
|
public function getList() |
|
16
|
|
|
{ |
|
17
|
|
|
$users = Manager::with('roles')->paginate(); |
|
|
|
|
|
|
18
|
|
|
return response()->json($users); |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
public function save(ManagerRequest $request) |
|
22
|
|
|
{ |
|
23
|
|
|
if (empty($request->input('id'))) { |
|
24
|
|
|
$model = new Manager(); |
|
25
|
|
|
} else { |
|
26
|
|
|
$model = Manager::findOrFail($request->input('id')); |
|
27
|
|
|
} |
|
28
|
|
|
$model->name = $request->input('name'); |
|
29
|
|
|
$model->email = $request->input('email'); |
|
30
|
|
|
if (!empty($request->input('password'))) { |
|
31
|
|
|
$model->password = $request->input('password'); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
$model->save(); |
|
35
|
|
|
|
|
36
|
|
|
return response()->json(['message' => 'ok']); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
View Code Duplication |
public function delete($id) |
|
|
|
|
|
|
40
|
|
|
{ |
|
41
|
|
|
if ($id == 1) { |
|
42
|
|
|
throw new AdminHttpException('超级管理员不能删除'); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
$model = Manager::findOrFail($id); |
|
46
|
|
|
$model->delete(); |
|
47
|
|
|
return response()->json(['message' => 'ok']); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function saveRole(Request $request) |
|
51
|
|
|
{ |
|
52
|
|
|
$user = Manager::findOrFail($request->input('id')); |
|
53
|
|
|
$user->roles()->sync($request->input('roles')); |
|
54
|
|
|
|
|
55
|
|
|
return response()->json(['message' => 'ok']); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public function getAllRole() |
|
59
|
|
|
{ |
|
60
|
|
|
$roles = Role::all(); |
|
61
|
|
|
return response()->json($roles); |
|
|
|
|
|
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: