orderBy('id','desc')->paginate(35); return view('maintenance.role.index',['roles'=>$roles]); } /** * Show the form for creating a new resource. * * @return Response */ public function create() { if(!Gate::allows('角色-录入')){ return redirect(url('/')); } $authoritiesAll=Authority::orderBy('alias_name','desc')->get(); return view('maintenance.role.create',compact('authoritiesAll')); } /** * Store a newly created resource in storage. * * @param Request $request * @return Response */ public function store(Request $request) { if(!Gate::allows('角色-录入')){ return redirect(url('/')); } $this->validatorCreate($request->all())->validate(); $role=new Role($request->all()); $role->save(); $authorityIds=$request->input('authority')??''; if($authorityIds){ $authorityIdArr=explode(',',$authorityIds); $role->authorities()->sync($authorityIdArr); } $this->log(__METHOD__,__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']); return redirect('maintenance/role/create')->with('successTip',"成功录入角色“{$request->input('name')}”"); } protected function validatorCreate(array $data) { return Validator::make($data, [ 'name' => ['required', 'string', 'max:50', 'unique:roles'], ]); } protected function validatorUpdate(array $data) { return Validator::make($data, [ 'name' => ['required', 'string', 'max:50'], ]); } /** * Display the specified resource. * * @param Role $role * @return Response */ public function show(Role $role) { // } /** * Show the form for editing the specified resource. * * @param Role $role * @return Response */ public function edit(Role $role) { if(!Gate::allows('角色-编辑')){ return redirect(url('/')); } $authoritiesAll=Authority::all(); $authorities=$role->authorities()->get(); return view('maintenance.role.edit',compact('role','authorities','authoritiesAll')); } /** * Update the specified resource in storage. * * @param Request $request * @param Role $role * @return Response */ public function update(Request $request, Role $role) { if(!Gate::allows('角色-编辑')){ return redirect(url('/')); } $this->validatorUpdate($request->all())->validate(); $role->fill($request->all()); $role->update(); $authorityIds=$request->input('authority')??''; if($authorityIds){ $authorityIdArr=explode(',',$authorityIds); $role->authorities()->sync($authorityIdArr); }else{ $role->authorities()->sync([]); } $this->log(__METHOD__,__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']); return redirect('maintenance/role/')->with('successTip',"成功修改角色“{$role['name']}”!"); } /** * Remove the specified resource from storage. * * @param Role $role * @return array|Response * @throws Exception */ public function destroy(Role $role) { if(!Gate::allows('角色-删除')){ return redirect(url('/')); } $this->log(__METHOD__,__FUNCTION__,$role->toJson(),Auth::user()['id']); $re=$role->delete(); return ['success'=>$re]; } }