Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/261.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在Laravel 5中存储用户角色权限的最佳方式是什么?_Php_Laravel_Roles_Privileges - Fatal编程技术网

Php 在Laravel 5中存储用户角色权限的最佳方式是什么?

Php 在Laravel 5中存储用户角色权限的最佳方式是什么?,php,laravel,roles,privileges,Php,Laravel,Roles,Privileges,我正在用Laravel 5.2构建一个web应用程序。我对Laravel比较陌生,希望遵循最佳实践。 我有一个名为Roles的表,其中有几个命名用户角色(即:admin、editor等)。我希望管理员能够编辑这些角色的权限并创建新角色。存储权限的最佳方式是什么 是否使用整数字段,每个位表示一个权限?(我认为这很快就会失控) 是否使用数据透视表和多个权限连接 使用字符串字段并仅序列化所选权限 将来可能会添加新的权限,我的目标是能够轻松确定用户是否具有特定角色。例如:$user->roles->ha

我正在用Laravel 5.2构建一个web应用程序。我对Laravel比较陌生,希望遵循最佳实践。 我有一个名为Roles的表,其中有几个命名用户角色(即:admin、editor等)。我希望管理员能够编辑这些角色的权限并创建新角色。存储权限的最佳方式是什么

  • 是否使用整数字段,每个位表示一个权限?(我认为这很快就会失控)
  • 是否使用数据透视表和多个权限连接
  • 使用字符串字段并仅序列化所选权限

  • 将来可能会添加新的权限,我的目标是能够轻松确定用户是否具有特定角色。例如:$user->roles->hasAdmin()或类似的东西。

    您可能需要从包中借用角色/权限表的最佳实践:


    Laravel 5.2已过时,不再受支持。如果你正在开发新的应用程序,你应该瞄准Laravel5.4。谢谢,这正是我所需要的!我不需要每个用户都有多个角色,但不管怎样,如果将来需要的话,它仍然很有用。
        // Create table for storing roles
        Schema::create('{{ $rolesTable }}', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name')->unique();
            $table->string('display_name')->nullable();
            $table->string('description')->nullable();
            $table->timestamps();
        });
    
        // Create table for associating roles to users (Many-to-Many)
        Schema::create('{{ $roleUserTable }}', function (Blueprint $table) {
            $table->integer('user_id')->unsigned();
            $table->integer('role_id')->unsigned();
    
            $table->foreign('user_id')->references('{{ $userKeyName }}')->on('{{ $usersTable }}')
                ->onUpdate('cascade')->onDelete('cascade');
            $table->foreign('role_id')->references('id')->on('{{ $rolesTable }}')
                ->onUpdate('cascade')->onDelete('cascade');
    
            $table->primary(['user_id', 'role_id']);
        });
    
        // Create table for storing permissions
        Schema::create('{{ $permissionsTable }}', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name')->unique();
            $table->string('display_name')->nullable();
            $table->string('description')->nullable();
            $table->timestamps();
        });
    
        // Create table for associating permissions to roles (Many-to-Many)
        Schema::create('{{ $permissionRoleTable }}', function (Blueprint $table) {
            $table->integer('permission_id')->unsigned();
            $table->integer('role_id')->unsigned();
    
            $table->foreign('permission_id')->references('id')->on('{{ $permissionsTable }}')
                ->onUpdate('cascade')->onDelete('cascade');
            $table->foreign('role_id')->references('id')->on('{{ $rolesTable }}')
                ->onUpdate('cascade')->onDelete('cascade');
    
            $table->primary(['permission_id', 'role_id']);
        });