1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Http\Controllers\Admin; |
4
|
|
|
|
5
|
|
|
use App\Entities\User; |
6
|
|
|
use App\Repositories\Criteria\Like; |
7
|
|
|
use App\Repositories\Criteria\Where; |
8
|
|
|
use App\Repositories\UserRepository; |
9
|
|
|
use Illuminate\Database\Eloquent\Collection; |
10
|
|
|
use Illuminate\Database\Eloquent\MassAssignmentException; |
11
|
|
|
|
12
|
|
|
class UserController extends DataController |
13
|
|
|
{ |
14
|
|
|
public function index() |
15
|
|
|
{ |
16
|
|
|
if (request()->filled('id')) { |
17
|
|
|
$this->repository->pushCriteria(new Where('id', request('id'))); |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
if (request()->filled('name')) { |
21
|
|
|
$this->repository->pushCriteria(new Like('username', request('name'))); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
if (request()->filled('email')) { |
25
|
|
|
$this->repository->pushCriteria(new Like('email', request('email'))); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
if (request()->filled('status')) { |
29
|
|
|
$this->repository->pushCriteria(new Where('status', request('status'))); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** @var Collection $models */ |
33
|
|
|
$models = parent::index(); |
34
|
|
|
$models->load('roles'); |
35
|
|
|
|
36
|
|
|
foreach ($models as $model) { |
37
|
|
|
$model->roles_edit = $model->roles->map(function ($role) { |
38
|
|
|
return $role->id; |
39
|
|
|
}); |
40
|
|
|
$model->addHidden('roles'); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
return $models; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function update($id) |
47
|
|
|
{ |
48
|
|
|
/** @var User $model */ |
49
|
|
|
$model = $this->repository->findOrFail($id); |
50
|
|
|
|
51
|
|
|
try { |
52
|
|
|
$model->fill(request()->all()); |
53
|
|
|
if (request('password')) { |
54
|
|
|
$model->password = app('hash')->make(request('password')); |
|
|
|
|
55
|
|
|
} |
56
|
|
|
$model->save(); |
57
|
|
|
} catch (MassAssignmentException $e) { |
58
|
|
|
app('log')->error($e->getMessage()); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$model->roles()->sync(request('roles_edit')); |
62
|
|
|
|
63
|
|
|
return $model; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
protected function getRepository() |
67
|
|
|
{ |
68
|
|
|
return UserRepository::class; |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|