开发环境搭建
-
安装xampp
-
设置web文件夹:
C:\>d: D:\>mkdir laravel D:\>cd laravel D:\laravel>_
-
修改hosts文件:
127.0.0.1 permission1.laravel.local
-
修改apache\conf\extra\httpd-vhosts.conf:
NameVirtualHost *:80 <VirtualHost *:80> DocumentRoot "d:/laravel/permission1/public" ServerName permission1.laravel.local <Directory "d:/laravel/permission1/public"> Options Indexes FollowSymLinks Includes ExecCGI AllowOverride All Require all granted </Directory> </VirtualHost>
-
-
安装composer
-
设置国内源:
composer config -g repo.packagist composer https://mirrors.aliyun.com/composer/
-
取消源设置:
composer config -g --unset repos.packagist
-
查看:
composer config -g -l
-
-
-
查看默认源
npm config get registry
-
修改为国内淘宝源
npm config set registry https://registry.npm.taobao.org
-
创建Laravel项目
-
运行以下命令来创建一个新的Laravel项目
composer create-project laravel/laravel permission1
-
查看当前项目的Laravel版本号
php artisan --version
初始化数据库
-
创建项目数据库
create database permission1
-
修改.env
.... APP_URL=http://permission1.laravel.local .... DB_DATABASE=permission1 ....
安装Spatie权限包
-
为ACL功能安装Spatie包以使用它的方法,同时安装表单集合包。打开您的终端并执行以下命令
composer require spatie/laravel-permission composer require laravelcollective/html
-
打开
config/app.php
文件,添加自动加载服务器和别名。config/app.php
'providers' => [ .... Spatie\Permission\PermissionServiceProvider::class, ],
-
可以根据需求定制Spatie包。如果有需要,运行以下命令获取config/permission.php中的配置文件和迁移文件。
php artisan vendor:publish --provider="Spatie\Permission\PermissionServiceProvider"
-
现在应该会看到permission.php文件和迁移文件。使用以下命令执行迁移:
php artisan migrate
-
相关数据表说明:
- roles —— 角色的模型表;
- permissions —— 权限的模型表;
- model_has_roles —— 模型与角色的关联表,用户拥有什么角色在此表中定义,一个用户能拥有多个角色;
- role_has_permissions —— 角色拥有的权限关联表,如管理员拥有查看后台的权限都是在此表定义,一个角色能拥有多个权限
- model_has_permissions —— 模型与权限关联表,一个模型能拥有多个权限。
创建产品模型和迁移
-
使用以下命令为products表创建一个迁移:
php artisan make:model Product -m
-
修改产品迁移文件:
database/migrations/create_products_table.php
<?php use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; return new class extends Migration { /** * Run the migrations. */ public function up(): void { Schema::create('products', function (Blueprint $table) { $table->id(); $table->string('name'); $table->text('detail'); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down(): void { Schema::dropIfExists('products'); } };
-
执行迁移:
php artisan migrate
更新模型
-
修改User模型相应代码:
app/Models/User.php
<?php namespace App\Models; // use Illuminate\Contracts\Auth\MustVerifyEmail; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Notifications\Notifiable; use Laravel\Sanctum\HasApiTokens; use Spatie\Permission\Traits\HasRoles; class User extends Authenticatable { use HasApiTokens, HasFactory, Notifiable, HasRoles; /** * The attributes that are mass assignable. * * @var array<int, string> */ protected $fillable = [ 'name', 'email', 'password', ]; /** * The attributes that should be hidden for serialization. * * @var array<int, string> */ protected $hidden = [ 'password', 'remember_token', ]; /** * The attributes that should be cast. * * @var array<string, string> */ protected $casts = [ 'email_verified_at' => 'datetime', 'password' => 'hashed', ]; }
-
修改Product模型相应代码:
app/Models/Product.php
<?php namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; class Product extends Model { use HasFactory; protected $fillable = [ 'name', 'detail' ]; }
注册中间件
- Spatie包带有内置中间件,使用起来很方便,如下所示:
role
permission
打开Kernel.php文件,按照以下步骤添加中间件:
app/Http/Kernel.php
// Laravel 9 uses $routeMiddleware = [
//protected $routeMiddleware = [
// Laravel 10+ uses $middlewareAliases = [
protected $middlewareAliases = [
// ...
'role' => \Spatie\Permission\Middlewares\RoleMiddleware::class,
'permission' => \Spatie\Permission\Middlewares\PermissionMiddleware::class,
'role_or_permission' => \Spatie\Permission\Middlewares\RoleOrPermissionMiddleware::class,
];
创建身份验证
-
要在Laravel 10应用程序中实现身份验证,需要执行以下步骤:
首先使用以下命令安装Laravel UI包:
composer require laravel/ui
-
接下来,使用Laravel UI命令在Laravel 10中生成授权脚手架。要执行此操作,请运行以下命令:
php artisan ui bootstrap --auth
-
使用 npm 命令安装模块
npm install npm run build
添加路由
- 要继续,我们需要为用户模块、产品模块和角色模块添加许多路由。此外,我们将使用具有角色和产品路由权限的中间件。要添加路线,请按照以下说明进行操作:
routes/web.php
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\HomeController;
use App\Http\Controllers\RoleController;
use App\Http\Controllers\UserController;
use App\Http\Controllers\ProductController;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider and all of them will
| be assigned to the "web" middleware group. Make something great!
|
*/
Route::get('/', function () {
return view('welcome');
});
Auth::routes();
Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');
Route::group(['middleware' => ['auth']], function() {
Route::resource('roles', RoleController::class);
Route::resource('users', UserController::class);
Route::resource('products', ProductController::class);
});
创建控制器
-
接下来为用户模块、产品模块和角色模块添加三个控制器。您可以创建三个控制器,如下所示:
php artisan make:controller UserController php artisan make:controller ProductController php artisan make:controller RoleController
-
app/Http/Controllers/UserController.php
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\User; use Spatie\Permission\Models\Role; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Hash; use Illuminate\Support\Arr; use Illuminate\View\View; use Illuminate\Http\RedirectResponse; class UserController extends Controller { /** * Display a listing of the resource. * * @return \Illuminate\Http\Response */ public function index(Request $request): View { $data = User::latest()->paginate(5); return view('users.index',compact('data')) ->with('i', ($request->input('page', 1) - 1) * 5); } /** * Show the form for creating a new resource. * * @return \Illuminate\Http\Response */ public function create(): View { $roles = Role::pluck('name','name')->all(); return view('users.create',compact('roles')); } /** * Store a newly created resource in storage. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function store(Request $request): RedirectResponse { $this->validate($request, [ 'name' => 'required', 'email' => 'required|email|unique:users,email', 'password' => 'required|same:confirm-password', 'roles' => 'required' ]); $input = $request->all(); $input['password'] = Hash::make($input['password']); $user = User::create($input); $user->assignRole($request->input('roles')); return redirect()->route('users.index') ->with('success','User created successfully'); } /** * Display the specified resource. * * @param int $id * @return \Illuminate\Http\Response */ public function show($id): View { $user = User::find($id); return view('users.show',compact('user')); } /** * Show the form for editing the specified resource. * * @param int $id * @return \Illuminate\Http\Response */ public function edit($id): View { $user = User::find($id); $roles = Role::pluck('name','name')->all(); $userRole = $user->roles->pluck('name','name')->all(); return view('users.edit',compact('user','roles','userRole')); } /** * Update the specified resource in storage. * * @param \Illuminate\Http\Request $request * @param int $id * @return \Illuminate\Http\Response */ public function update(Request $request, $id): RedirectResponse { $this->validate($request, [ 'name' => 'required', 'email' => 'required|email|unique:users,email,'.$id, 'password' => 'same:confirm-password', 'roles' => 'required' ]); $input = $request->all(); if(!empty($input['password'])){ $input['password'] = Hash::make($input['password']); }else{ $input = Arr::except($input,array('password')); } $user = User::find($id); $user->update($input); DB::table('model_has_roles')->where('model_id',$id)->delete(); $user->assignRole($request->input('roles')); return redirect()->route('users.index') ->with('success','User updated successfully'); } /** * Remove the specified resource from storage. * * @param int $id * @return \Illuminate\Http\Response */ public function destroy($id): RedirectResponse { User::find($id)->delete(); return redirect()->route('users.index') ->with('success','User deleted successfully'); } }
-
app/Http/Controllers/ProductController.php
<?php namespace App\Http\Controllers; use App\Models\Product; use Illuminate\Http\Request; use Illuminate\View\View; use Illuminate\Http\RedirectResponse; class ProductController extends Controller { /** * Display a listing of the resource. * * @return \Illuminate\Http\Response */ function __construct() { $this->middleware('permission:product-list|product-create|product-edit|product-delete', ['only' => ['index','show']]); $this->middleware('permission:product-create', ['only' => ['create','store']]); $this->middleware('permission:product-edit', ['only' => ['edit','update']]); $this->middleware('permission:product-delete', ['only' => ['destroy']]); } /** * Display a listing of the resource. * * @return \Illuminate\Http\Response */ public function index(): View { $products = Product::latest()->paginate(5); return view('products.index',compact('products')) ->with('i', (request()->input('page', 1) - 1) * 5); } /** * Show the form for creating a new resource. * * @return \Illuminate\Http\Response */ public function create(): View { return view('products.create'); } /** * Store a newly created resource in storage. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function store(Request $request): RedirectResponse { request()->validate([ 'name' => 'required', 'detail' => 'required', ]); Product::create($request->all()); return redirect()->route('products.index') ->with('success','Product created successfully.'); } /** * Display the specified resource. * * @param \App\Product $product * @return \Illuminate\Http\Response */ public function show(Product $product): View { return view('products.show',compact('product')); } /** * Show the form for editing the specified resource. * * @param \App\Product $product * @return \Illuminate\Http\Response */ public function edit(Product $product): View { return view('products.edit',compact('product')); } /** * Update the specified resource in storage. * * @param \Illuminate\Http\Request $request * @param \App\Product $product * @return \Illuminate\Http\Response */ public function update(Request $request, Product $product): RedirectResponse { request()->validate([ 'name' => 'required', 'detail' => 'required', ]); $product->update($request->all()); return redirect()->route('products.index') ->with('success','Product updated successfully'); } /** * Remove the specified resource from storage. * * @param \App\Product $product * @return \Illuminate\Http\Response */ public function destroy(Product $product): RedirectResponse { $product->delete(); return redirect()->route('products.index') ->with('success','Product deleted successfully'); } }
-
app/Http/Controllers/RoleController.php
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Spatie\Permission\Models\Role; use Spatie\Permission\Models\Permission; use Illuminate\Support\Facades\DB; use Illuminate\View\View; use Illuminate\Http\RedirectResponse; class RoleController extends Controller { /** * Display a listing of the resource. * * @return \Illuminate\Http\Response */ function __construct() { $this->middleware('permission:role-list|role-create|role-edit|role-delete', ['only' => ['index','store']]); $this->middleware('permission:role-create', ['only' => ['create','store']]); $this->middleware('permission:role-edit', ['only' => ['edit','update']]); $this->middleware('permission:role-delete', ['only' => ['destroy']]); } /** * Display a listing of the resource. * * @return \Illuminate\Http\Response */ public function index(Request $request): View { $roles = Role::orderBy('id','DESC')->paginate(5); return view('roles.index',compact('roles')) ->with('i', ($request->input('page', 1) - 1) * 5); } /** * Show the form for creating a new resource. * * @return \Illuminate\Http\Response */ public function create(): View { $permission = Permission::get(); return view('roles.create',compact('permission')); } /** * Store a newly created resource in storage. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function store(Request $request): RedirectResponse { $this->validate($request, [ 'name' => 'required|unique:roles,name', 'permission' => 'required', ]); $role = Role::create(['name' => $request->input('name')]); $role->syncPermissions($request->input('permission')); return redirect()->route('roles.index') ->with('success','Role created successfully'); } /** * Display the specified resource. * * @param int $id * @return \Illuminate\Http\Response */ public function show($id): View { $role = Role::find($id); $rolePermissions = Permission::join("role_has_permissions","role_has_permissions.permission_id","=","permissions.id") ->where("role_has_permissions.role_id",$id) ->get(); return view('roles.show',compact('role','rolePermissions')); } /** * Show the form for editing the specified resource. * * @param int $id * @return \Illuminate\Http\Response */ public function edit($id): View { $role = Role::find($id); $permission = Permission::get(); $rolePermissions = DB::table("role_has_permissions")->where("role_has_permissions.role_id",$id) ->pluck('role_has_permissions.permission_id','role_has_permissions.permission_id') ->all(); return view('roles.edit',compact('role','permission','rolePermissions')); } /** * Update the specified resource in storage. * * @param \Illuminate\Http\Request $request * @param int $id * @return \Illuminate\Http\Response */ public function update(Request $request, $id): RedirectResponse { $this->validate($request, [ 'name' => 'required', 'permission' => 'required', ]); $role = Role::find($id); $role->name = $request->input('name'); $role->save(); $role->syncPermissions($request->input('permission')); return redirect()->route('roles.index') ->with('success','Role updated successfully'); } /** * Remove the specified resource from storage. * * @param int $id * @return \Illuminate\Http\Response */ public function destroy($id): RedirectResponse { DB::table("roles")->where('id',$id)->delete(); return redirect()->route('roles.index') ->with('success','Role deleted successfully'); } }
创建模板文件
- 创建前端模块,用spatie权限包检查laravel中的用户角色和权限。模板文件包括:
主题布局
- app.blade.php
用户模块
- index.blade.php
- create.blade.php
- edit.blade.php
- show.blade.php
角色模块
- index.blade.php
- create.blade.php
- edit.blade.php
- show.blade.php
产品模块
-
index.blade.php
-
create.blade.php
-
edit.blade.php
-
show.blade.php
-
resources/views/layouts/app.blade.php
<!doctype html> <html lang="{{ str_replace('_', '-', app()->getLocale()) }}"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <!-- CSRF Token --> <meta name="csrf-token" content="{{ csrf_token() }}"> <title>{{ config('app.name', 'Laravel') }}</title> <!-- Fonts --> <link rel="dns-prefetch" href="//fonts.gstatic.com"> <link href="https://fonts.bunny.net/css?family=Nunito" rel="stylesheet"> <!-- Scripts --> @vite(['resources/sass/app.scss', 'resources/js/app.js']) </head> <body> <div id="app"> <nav class="navbar navbar-expand-md navbar-light bg-white shadow-sm"> <div class="container"> <a class="navbar-brand" href="{{ url('/') }}"> Step-by-Step Guide to User Role and Permission Tutorial in Laravel 10 - LaravelTuts.com </a> <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="{{ __('Toggle navigation') }}"> <span class="navbar-toggler-icon"></span> </button> <div class="collapse navbar-collapse" id="navbarSupportedContent"> <!-- Left Side Of Navbar --> <ul class="navbar-nav me-auto"> </ul> <!-- Right Side Of Navbar --> <ul class="navbar-nav ms-auto"> <!-- Authentication Links --> @guest @if (Route::has('login')) <li class="nav-item"> <a class="nav-link" href="{{ route('login') }}">{{ __('Login') }}</a> </li> @endif @if (Route::has('register')) <li class="nav-item"> <a class="nav-link" href="{{ route('register') }}">{{ __('Register') }}</a> </li> @endif @else <li><a class="nav-link" href="{{ route('users.index') }}">Manage Users</a></li> <li><a class="nav-link" href="{{ route('roles.index') }}">Manage Role</a></li> <li><a class="nav-link" href="{{ route('products.index') }}">Manage Product</a></li> <li class="nav-item dropdown"> <a id="navbarDropdown" class="nav-link dropdown-toggle" href="#" role="button" data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false" v-pre> {{ Auth::user()->name }} </a> <div class="dropdown-menu dropdown-menu-end" aria-labelledby="navbarDropdown"> <a class="dropdown-item" href="{{ route('logout') }}" οnclick="event.preventDefault(); document.getElementById('logout-form').submit();"> {{ __('Logout') }} </a> <form id="logout-form" action="{{ route('logout') }}" method="POST" class="d-none"> @csrf </form> </div> </li> @endguest </ul> </div> </div> </nav> <main class="py-4"> <div class="container"> <div class="row justify-content-center"> <div class="col-md-12"> <div class="card"> <div class="card-body"> @yield('content') </div> </div> </div> </div> </div> </main> </div> </body> </html>
-
resources/views/users/index.blade.php
@extends('layouts.app') @section('content') <div class="row"> <div class="col-lg-12 margin-tb"> <div class="pull-left"> <h2>Users Management</h2> </div> <div class="pull-right"> <a class="btn btn-success" href="{{ route('users.create') }}"> Create New User</a> </div> </div> </div> @if ($message = Session::get('success')) <div class="alert alert-success"> <p>{{ $message }}</p> </div> @endif <table class="table table-bordered"> <tr> <th>No</th> <th>Name</th> <th>Email</th> <th>Roles</th> <th width="280px">Action</th> </tr> @foreach ($data as $key => $user) <tr> <td>{{ ++$i }}</td> <td>{{ $user->name }}</td> <td>{{ $user->email }}</td> <td> @if(!empty($user->getRoleNames())) @foreach($user->getRoleNames() as $v) <label class="badge text-bg-success">{{ $v }}</label> @endforeach @endif </td> <td> <a class="btn btn-info" href="{{ route('users.show',$user->id) }}">Show</a> <a class="btn btn-primary" href="{{ route('users.edit',$user->id) }}">Edit</a> {!! Form::open(['method' => 'DELETE','route' => ['users.destroy', $user->id],'style'=>'display:inline']) !!} {!! Form::submit('Delete', ['class' => 'btn btn-danger']) !!} {!! Form::close() !!} </td> </tr> @endforeach </table> {!! $data->render() !!} <p class="text-center text-primary"><small>Tutorial by LaravelTut.com</small></p> @endsection
-
resources/views/users/create.blade.php
@extends('layouts.app') @section('content') <div class="row"> <div class="col-lg-12 margin-tb"> <div class="pull-left"> <h2>Create New User</h2> </div> <div class="pull-right"> <a class="btn btn-primary" href="{{ route('users.index') }}"> Back</a> </div> </div> </div> @if (count($errors) > 0) <div class="alert alert-danger"> <strong>Whoops!</strong> There were some problems with your input.<br><br> <ul> @foreach ($errors->all() as $error) <li>{{ $error }}</li> @endforeach </ul> </div> @endif {!! Form::open(array('route' => 'users.store','method'=>'POST')) !!} <div class="row"> <div class="col-xs-12 col-sm-12 col-md-12"> <div class="form-group"> <strong>Name:</strong> {!! Form::text('name', null, array('placeholder' => 'Name','class' => 'form-control')) !!} </div> </div> <div class="col-xs-12 col-sm-12 col-md-12"> <div class="form-group"> <strong>Email:</strong> {!! Form::text('email', null, array('placeholder' => 'Email','class' => 'form-control')) !!} </div> </div> <div class="col-xs-12 col-sm-12 col-md-12"> <div class="form-group"> <strong>Password:</strong> {!! Form::password('password', array('placeholder' => 'Password','class' => 'form-control')) !!} </div> </div> <div class="col-xs-12 col-sm-12 col-md-12"> <div class="form-group"> <strong>Confirm Password:</strong> {!! Form::password('confirm-password', array('placeholder' => 'Confirm Password','class' => 'form-control')) !!} </div> </div> <div class="col-xs-12 col-sm-12 col-md-12"> <div class="form-group"> <strong>Role:</strong> {!! Form::select('roles[]', $roles,[], array('class' => 'form-control','multiple')) !!} </div> </div> <div class="col-xs-12 col-sm-12 col-md-12 text-center"> <button type="submit" class="btn btn-primary">Submit</button> </div> </div> {!! Form::close() !!} <p class="text-center text-primary"><small>Tutorial by LaravelTut.com</small></p> @endsection
-
resources/views/users/edit.blade.php
@extends('layouts.app') @section('content') <div class="row"> <div class="col-lg-12 margin-tb"> <div class="pull-left"> <h2>Edit New User</h2> </div> <div class="pull-right"> <a class="btn btn-primary" href="{{ route('users.index') }}"> Back</a> </div> </div> </div> @if (count($errors) > 0) <div class="alert alert-danger"> <strong>Whoops!</strong> There were some problems with your input.<br><br> <ul> @foreach ($errors->all() as $error) <li>{{ $error }}</li> @endforeach </ul> </div> @endif {!! Form::model($user, ['method' => 'PATCH','route' => ['users.update', $user->id]]) !!} <div class="row"> <div class="col-xs-12 col-sm-12 col-md-12"> <div class="form-group"> <strong>Name:</strong> {!! Form::text('name', null, array('placeholder' => 'Name','class' => 'form-control')) !!} </div> </div> <div class="col-xs-12 col-sm-12 col-md-12"> <div class="form-group"> <strong>Email:</strong> {!! Form::text('email', null, array('placeholder' => 'Email','class' => 'form-control')) !!} </div> </div> <div class="col-xs-12 col-sm-12 col-md-12"> <div class="form-group"> <strong>Password:</strong> {!! Form::password('password', array('placeholder' => 'Password','class' => 'form-control')) !!} </div> </div> <div class="col-xs-12 col-sm-12 col-md-12"> <div class="form-group"> <strong>Confirm Password:</strong> {!! Form::password('confirm-password', array('placeholder' => 'Confirm Password','class' => 'form-control')) !!} </div> </div> <div class="col-xs-12 col-sm-12 col-md-12"> <div class="form-group"> <strong>Role:</strong> {!! Form::select('roles[]', $roles,$userRole, array('class' => 'form-control','multiple')) !!} </div> </div> <div class="col-xs-12 col-sm-12 col-md-12 text-center"> <button type="submit" class="btn btn-primary">Submit</button> </div> </div> {!! Form::close() !!} <p class="text-center text-primary"><small>Tutorial by LaravelTuts.com</small></p> @endsection
-
resources/views/users/show.blade.php
@extends('layouts.app') @section('content') <div class="row"> <div class="col-lg-12 margin-tb"> <div class="pull-left"> <h2> Show User</h2> </div> <div class="pull-right"> <a class="btn btn-primary" href="{{ route('users.index') }}"> Back</a> </div> </div> </div> <div class="row"> <div class="col-xs-12 col-sm-12 col-md-12"> <div class="form-group"> <strong>Name:</strong> {{ $user->name }} </div> </div> <div class="col-xs-12 col-sm-12 col-md-12"> <div class="form-group"> <strong>Email:</strong> {{ $user->email }} </div> </div> <div class="col-xs-12 col-sm-12 col-md-12"> <div class="form-group"> <strong>Roles:</strong> @if(!empty($user->getRoleNames())) @foreach($user->getRoleNames() as $v) <label class="badge badge-success">{{ $v }}</label> @endforeach @endif </div> </div> </div> @endsection
-
resources/views/roles/index.blade.php
@extends('layouts.app') @section('content') <div class="row"> <div class="col-lg-12 margin-tb"> <div class="pull-left"> <h2>Role Management</h2> </div> <div class="pull-right"> @can('role-create') <a class="btn btn-success" href="{{ route('roles.create') }}"> Create New Role</a> @endcan </div> </div> </div> @if ($message = Session::get('success')) <div class="alert alert-success"> <p>{{ $message }}</p> </div> @endif <table class="table table-bordered"> <tr> <th>No</th> <th>Name</th> <th width="280px">Action</th> </tr> @foreach ($roles as $key => $role) <tr> <td>{{ ++$i }}</td> <td>{{ $role->name }}</td> <td> <a class="btn btn-info" href="{{ route('roles.show',$role->id) }}">Show</a> @can('role-edit') <a class="btn btn-primary" href="{{ route('roles.edit',$role->id) }}">Edit</a> @endcan @can('role-delete') {!! Form::open(['method' => 'DELETE','route' => ['roles.destroy', $role->id],'style'=>'display:inline']) !!} {!! Form::submit('Delete', ['class' => 'btn btn-danger']) !!} {!! Form::close() !!} @endcan </td> </tr> @endforeach </table> {!! $roles->render() !!} <p class="text-center text-primary"><small>Tutorial by LaravelTuts.com</small></p> @endsection
-
resources/views/roles/create.blade.php
@extends('layouts.app') @section('content') <div class="row"> <div class="col-lg-12 margin-tb"> <div class="pull-left"> <h2>Create New Role</h2> </div> <div class="pull-right"> <a class="btn btn-primary" href="{{ route('roles.index') }}"> Back</a> </div> </div> </div> @if (count($errors) > 0) <div class="alert alert-danger"> <strong>Whoops!</strong> There were some problems with your input.<br><br> <ul> @foreach ($errors->all() as $error) <li>{{ $error }}</li> @endforeach </ul> </div> @endif {!! Form::open(array('route' => 'roles.store','method'=>'POST')) !!} <div class="row"> <div class="col-xs-12 col-sm-12 col-md-12"> <div class="form-group"> <strong>Name:</strong> {!! Form::text('name', null, array('placeholder' => 'Name','class' => 'form-control')) !!} </div> </div> <div class="col-xs-12 col-sm-12 col-md-12"> <div class="form-group"> <strong>Permission:</strong> <br/> @foreach($permission as $value) <label>{{ Form::checkbox('permission[]', $value->id, false, array('class' => 'name')) }} {{ $value->name }}</label> <br/> @endforeach </div> </div> <div class="col-xs-12 col-sm-12 col-md-12 text-center"> <button type="submit" class="btn btn-primary">Submit</button> </div> </div> {!! Form::close() !!} <p class="text-center text-primary"><small>Tutorial by LaravelTuts.com</small></p> @endsection
-
resources/views/roles/edit.blade.php
@extends('layouts.app') @section('content') <div class="row"> <div class="col-lg-12 margin-tb"> <div class="pull-left"> <h2>Edit Role</h2> </div> <div class="pull-right"> <a class="btn btn-primary" href="{{ route('roles.index') }}"> Back</a> </div> </div> </div> @if (count($errors) > 0) <div class="alert alert-danger"> <strong>Whoops!</strong> There were some problems with your input.<br><br> <ul> @foreach ($errors->all() as $error) <li>{{ $error }}</li> @endforeach </ul> </div> @endif {!! Form::model($role, ['method' => 'PATCH','route' => ['roles.update', $role->id]]) !!} <div class="row"> <div class="col-xs-12 col-sm-12 col-md-12"> <div class="form-group"> <strong>Name:</strong> {!! Form::text('name', null, array('placeholder' => 'Name','class' => 'form-control')) !!} </div> </div> <div class="col-xs-12 col-sm-12 col-md-12"> <div class="form-group"> <strong>Permission:</strong> <br/> @foreach($permission as $value) <label>{{ Form::checkbox('permission[]', $value->id, in_array($value->id, $rolePermissions) ? true : false, array('class' => 'name')) }} {{ $value->name }}</label> <br/> @endforeach </div> </div> <div class="col-xs-12 col-sm-12 col-md-12 text-center"> <button type="submit" class="btn btn-primary">Submit</button> </div> </div> {!! Form::close() !!} @endsection <p class="text-center text-primary"><small>Tutorial by LaravelTuts.com</small></p>
-
resources/views/roles/show.blade.php
@extends('layouts.app') @section('content') <div class="row"> <div class="col-lg-12 margin-tb"> <div class="pull-left"> <h2> Show Role</h2> </div> <div class="pull-right"> <a class="btn btn-primary" href="{{ route('roles.index') }}"> Back</a> </div> </div> </div> <div class="row"> <div class="col-xs-12 col-sm-12 col-md-12"> <div class="form-group"> <strong>Name:</strong> {{ $role->name }} </div> </div> <div class="col-xs-12 col-sm-12 col-md-12"> <div class="form-group"> <strong>Permissions:</strong> @if(!empty($rolePermissions)) @foreach($rolePermissions as $v) <label class="label label-success">{{ $v->name }},</label> @endforeach @endif </div> </div> </div> @endsection
-
resources/views/products/index.blade.php
@extends('layouts.app') @section('content') <div class="row"> <div class="col-lg-12 margin-tb"> <div class="pull-left"> <h2>Products</h2> </div> <div class="pull-right"> @can('product-create') <a class="btn btn-success" href="{{ route('products.create') }}"> Create New Product</a> @endcan </div> </div> </div> @if ($message = Session::get('success')) <div class="alert alert-success"> <p>{{ $message }}</p> </div> @endif <table class="table table-bordered"> <tr> <th>No</th> <th>Name</th> <th>Details</th> <th width="280px">Action</th> </tr> @foreach ($products as $product) <tr> <td>{{ ++$i }}</td> <td>{{ $product->name }}</td> <td>{{ $product->detail }}</td> <td> <form action="{{ route('products.destroy',$product->id) }}" method="POST"> <a class="btn btn-info" href="{{ route('products.show',$product->id) }}">Show</a> @can('product-edit') <a class="btn btn-primary" href="{{ route('products.edit',$product->id) }}">Edit</a> @endcan @csrf @method('DELETE') @can('product-delete') <button type="submit" class="btn btn-danger">Delete</button> @endcan </form> </td> </tr> @endforeach </table> {!! $products->links() !!} <p class="text-center text-primary"><small>Tutorial by LaravelTuts.com</small></p> @endsection
-
resources/views/products/create.blade.php
@extends('layouts.app') @section('content') <div class="row"> <div class="col-lg-12 margin-tb"> <div class="pull-left"> <h2>Add New Product</h2> </div> <div class="pull-right"> <a class="btn btn-primary" href="{{ route('products.index') }}"> Back</a> </div> </div> </div> @if ($errors->any()) <div class="alert alert-danger"> <strong>Whoops!</strong> There were some problems with your input.<br><br> <ul> @foreach ($errors->all() as $error) <li>{{ $error }}</li> @endforeach </ul> </div> @endif <form action="{{ route('products.store') }}" method="POST"> @csrf <div class="row"> <div class="col-xs-12 col-sm-12 col-md-12"> <div class="form-group"> <strong>Name:</strong> <input type="text" name="name" class="form-control" placeholder="Name"> </div> </div> <div class="col-xs-12 col-sm-12 col-md-12"> <div class="form-group"> <strong>Detail:</strong> <textarea class="form-control" style="height:150px" name="detail" placeholder="Detail"></textarea> </div> </div> <div class="col-xs-12 col-sm-12 col-md-12 text-center"> <button type="submit" class="btn btn-primary">Submit</button> </div> </div> </form> <p class="text-center text-primary"><small>Tutorial by LaravelTuts.com</small></p> @endsection
-
resources/views/products/edit.blade.php
@extends('layouts.app') @section('content') <div class="row"> <div class="col-lg-12 margin-tb"> <div class="pull-left"> <h2>Edit Product</h2> </div> <div class="pull-right"> <a class="btn btn-primary" href="{{ route('products.index') }}"> Back</a> </div> </div> </div> @if ($errors->any()) <div class="alert alert-danger"> <strong>Whoops!</strong> There were some problems with your input.<br><br> <ul> @foreach ($errors->all() as $error) <li>{{ $error }}</li> @endforeach </ul> </div> @endif <form action="{{ route('products.update',$product->id) }}" method="POST"> @csrf @method('PUT') <div class="row"> <div class="col-xs-12 col-sm-12 col-md-12"> <div class="form-group"> <strong>Name:</strong> <input type="text" name="name" value="{{ $product->name }}" class="form-control" placeholder="Name"> </div> </div> <div class="col-xs-12 col-sm-12 col-md-12"> <div class="form-group"> <strong>Detail:</strong> <textarea class="form-control" style="height:150px" name="detail" placeholder="Detail">{{ $product->detail }}</textarea> </div> </div> <div class="col-xs-12 col-sm-12 col-md-12 text-center"> <button type="submit" class="btn btn-primary">Submit</button> </div> </div> </form> <p class="text-center text-primary"><small>Tutorial by LaravelTuts.com</small></p> @endsection
-
resources/views/products/show.blade.php
@extends('layouts.app') @section('content') <div class="row"> <div class="col-lg-12 margin-tb"> <div class="pull-left"> <h2> Show Product</h2> </div> <div class="pull-right"> <a class="btn btn-primary" href="{{ route('products.index') }}"> Back</a> </div> </div> </div> <div class="row"> <div class="col-xs-12 col-sm-12 col-md-12"> <div class="form-group"> <strong>Name:</strong> {{ $product->name }} </div> </div> <div class="col-xs-12 col-sm-12 col-md-12"> <div class="form-group"> <strong>Details:</strong> {{ $product->detail }} </div> </div> </div> @endsection <p class="text-center text-primary"><small>Tutorial by LaravelTuts.com</small></p>
为权限和超级用户创建种子
-
接下来为权限创建一个种子程序。使用下面列出的种子程序添加一组固定的权限。如果需要,也可以添加更多权限,就像:
-
role-list
-
role-create
-
role-edit
-
role-delete
-
product-list
-
product-create
-
product-edit
-
product-delete
-
使用以下命令创建种子程序:
php artisan make:seeder PermissionTableSeeder php artisan make:seeder CreateAdminUserSeeder
-
修改种子程序文件:
database/seeds/PermissionTableSeeder.php
<?php namespace Database\Seeders; use Illuminate\Database\Console\Seeds\WithoutModelEvents; use Illuminate\Database\Seeder; use Spatie\Permission\Models\Permission; class PermissionTableSeeder extends Seeder { /** * Run the database seeds. */ public function run(): void { $permissions = [ 'role-list', 'role-create', 'role-edit', 'role-delete', 'product-list', 'product-create', 'product-edit', 'product-delete' ]; foreach ($permissions as $permission) { Permission::create(['name' => $permission]); } } }
database/seeds/CreateAdminUserSeeder.php
<?php namespace Database\Seeders; use Illuminate\Database\Console\Seeds\WithoutModelEvents; use Illuminate\Database\Seeder; use App\Models\User; use Spatie\Permission\Models\Role; use Spatie\Permission\Models\Permission; class CreateAdminUserSeeder extends Seeder { /** * Run the database seeds. */ public function run(): void { $user = User::create([ 'name' => 'Nitin Pujari', 'email' => 'admin@me.com', 'password' => bcrypt('password') ]); $role = Role::create(['name' => 'Admin']); $permissions = Permission::pluck('id','id')->all(); $role->syncPermissions($permissions); $user->assignRole([$role->id]); } }
-
运行db:seed命令生成伪数据:
php artisan db:seed --class=PermissionTableSeeder
php artisan db:seed --class=CreateAdminUserSeeder