This is the sixth day of my participation in the August Text Challenge.More challenges in August
1. Display of background permission control
1.1. Role Model Add permissions
Role and permission relationship: belongsTomany
BelongsTomany (association model, table name of intermediate table, foreign key ID corresponding to this model, foreign key ID corresponding to association model)
// Many-to-many roles and permissions
public function nodes() {
// Parameter 1: correlation model
// Parameter 2: the name of the intermediate table
// Parameter 3: the external key ID corresponding to this model
// Parameter 4: the external key ID corresponding to the association model
return $this -> belongsToMany(Node::class, 'role__node'.'role_id'.'node_id');
}
Copy the code
1.2 Adding the Display Permission Route
Route::get('role/node/{role}'.'RoleController@node') -> name('role.node');
Copy the code
1.3 Displaying node controller methods
// Assign permissions to roles (get)
public function node(Role $role) {
// dump($role -> nodes -> toArray());
// Correlation model
// dump($role -> nodes() -> pluck('name', 'id') -> toArray());
// Read all permissions
$nodeAll = (new Node()) -> getAllList();
// Read the permissions of the current role (node)
$nodes = $role -> nodes() -> pluck('id') -> toArray();
return view('admin.role.node', compact('role'.'nodeAll'.'nodes'));
}
Copy the code
1.4 Displaying node Templates
<! --_meta separated as public template -->
<! DOCTYPEHTML>
<html>
<head>
<meta charset="utf-8">
<meta name="renderer" content="webkit|ie-comp|ie-stand">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="Width = device - width, initial - scale = 1, minimum - scale = 1.0, the maximum - scale = 1.0, user - scalable = no" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
<link rel="Bookmark" href="/favicon.ico" >
<link rel="Shortcut Icon" href="/favicon.ico" />
<link rel="stylesheet" type="text/css" href="/admin/static/h-ui/css/H-ui.min.css" />
<link rel="stylesheet" type="text/css" href="/admin/static/h-ui.admin/css/H-ui.admin.css" />
<link rel="stylesheet" type="text/css" href="/ admin/lib/Hui - iconfont / 1.0.8 / iconfont. CSS" />
<link rel="stylesheet" type="text/css" href="/admin/static/h-ui.admin/skin/default/skin.css" id="skin" />
<link rel="stylesheet" type="text/css" href="/admin/static/h-ui.admin/css/style.css" />
<title>Assign permissions</title>
</head>
<body>
<nav class="breadcrumb"><i class="Hui-iconfont"></i>Home page<span class="c-gray en">></span>The user center<span class="c-gray en">></span>Assign permissions<a class="btn btn-success radius r" style="The line - height: 1.6 em. margin-top:3px" href="javascript:location.replace(location.href);" title="Refresh"><i class="Hui-iconfont"></i></a></nav>
<article class="page-container">
<! -- Form validation -->
@include('admin.common.validate')
<form action="{{route('admin.role.node', $role)}}" method="post" class="form form-horizontal" id="form-member-add">
@csrf
@foreach($nodeAll as $item)
<div>
<input type="checkbox" name="node[]" value="{{$item['id']}}" id=""
@if(in_array($item['id'], $nodes)) checked @endif
>
{{$item['html']}} {{$item['name']}}
</div>
@endforeach
<div class="row cl">
<div class="col-xs-8 col-sm-9 col-xs-offset-4 col-sm-offset-3">
<input class="btn btn-primary radius" type="submit" value=" Assign permissions ">
</div>
</div>
</form>
</article>
<! --_footer separated as a public template -->
<script type="text/javascript" src="/ admin/lib/jquery / 1.9.1 / jquery. Min. Js." "></script>
<script type="text/javascript" src="/ admin/lib/layer / 2.4 / layer. The js." "></script>
<script type="text/javascript" src="/admin/static/h-ui/js/H-ui.min.js"></script>
<script type="text/javascript" src="/admin/static/h-ui.admin/js/H-ui.admin.js"></script> <! --/_footer as a public template -->
<! -- Please write the business related script of this page below -->
<script type="text/javascript" src="/ admin/lib/My97DatePicker / 4.8 / WdatePicker js." "></script>
<script type="text/javascript" src="/ admin/lib/jquery validation / 1.14.0 / jquery. Validate. Js." "></script>
<script type="text/javascript" src="/ admin/lib/jquery validation / 1.14.0 / validate - the methods. The js." "></script>
<script type="text/javascript" src="/ admin/lib/jquery validation / 1.14.0 / messages_zh js." "></script>
</body>
</html>
Copy the code
Second, background allocation permission logic
2.1 Controller Method
// Assign permissions to roles (post)
public function nodeSave(Request $request, Role $role) {
//dump($request -> all());
$role->nodes()->sync($request->get('node'));
return redirect(route('admin.role.index'));
}
Copy the code
2.2 the effect
If you find this article helpful on your way to learning PHP, please follow me to like and comment on it. Thank you, your blog is definitely another support for me to write.