1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- <?php
- namespace App\Http\Controllers\Admin;
- use App\Http\Controllers\Controller;
- use Illuminate\Http\Request;
- use App\Role;
- use App\Permission;
- class RoleController extends Controller
- {
- /**
- * @return \Illuminate\View\View
- */
- public function create()
- {
- $permissions = Permission::select('id','name','description','parent_id')->get();
- return view('admin/rolecreate', ['permissions' => $permissions]);
- }
- /**
- * @param Request $request
- * @return \Illuminate\Http\RedirectResponse
- */
- public function store(Request $request)
- {
- $this->validate($request, [
- 'name' => 'unique:role|required',
- 'permissions' => 'required'
- ],[
- 'name.required' => '角色名称不能为空',
- 'name.unique' => '角色名称已经存在',
- 'permissions.required' => '该角色权限不能为空',
- ]);
- $childs= $request->input('permissions');
- foreach($childs as $a){
- $role = new Role();
- $role->name = $request->input('name');
- $role->child = $a;
- $role->create_time = time();
- $role->save();
- }
- return redirect('/admin/permission/index?type=2')->with('info', '添加角色成功');
- }
- /**
- * @param $name
- * @return \Illuminate\View\View
- */
- public function edit($name)
- {
- $data['role'] = $name;
- $data['role_permissions'] = Role::select('child')->where('name', '=', $name)->get();
- $permissions = Permission::select('id','name','description','parent_id')->get();
- return view('admin/roleedit', ['permissions' => $permissions, 'data' => $data]);
- }
- /**
- * @param Request $request
- * @return \Illuminate\Http\RedirectResponse
- */
- public function update(Request $request)
- {
- $this->validate($request, [
- 'permissions' => 'required'
- ],[
- 'permissions.required' => '该角色权限不能为空',
- ]);
- $childs= $request->input('permissions');
- Role::where('name', '=', $request->input('name'))->delete();
- foreach($childs as $a){
- $role = new Role();
- $role->name = $request->input('name');
- $role->child = $a;
- $role->create_time = time();
- $role->save();
- }
- return redirect('/admin/permission/index?type=2')->with('info', '修改角色成功');
- }
- }
|