如何根据角色类别显示单独的许可?

发布于 2025-02-13 15:15:53 字数 1657 浏览 1 评论 0 原文

我是Laravel Development的新手。我正在使用Spatie在Laravel的角色和权限工作,要显示一个权限列表的标题名称,例如我有财产权限列表,然后想在权限列表的顶部显示属性标题名称,而其他我有代理权限列表然后想在权限列表的顶部展示代理标题名称。

我有权限的表,其中添加了 cole_category_id 的列,该列与其他表 roun_categories

“

prole_categories的表

.net/1b3pv.png“ rel =“ nofollow noreferrer”> “

位置

$permission = RoleCatgory::Leftjoin("permissions","role_categories.id","=","permissions.role_category_id")
    ->get();

@foreach($permission as $value)
  <h3>{{ $value->category_name }}</h3>
    @if(in_array($value['id'],$rolePermissions))
          @php
              $checked="checked";
           @endphp
    @else
           @php
              $checked="";
           @endphp
    @endif
        <div class="col-md-4 mb-2">
           <input type="checkbox" class="form-check-input" id="exampleCheck" name="permission[]" @php echo $checked; @endphp value="{{ $value->id }}">
           label class="form-check-label" for="exampleCheck">{{ $value->name }}</label>
        </div>
@endforeach

的控制器代码是我使用两个桌子的 在每个权限名称上,但只想在属性权限列表上显示一次

I am new in Laravel development. I am working on roles and permissions in Laravel using Spatie, want to display One title name of permission list e.g I have list of property permissions then want to display Property title name on the top of the permission list, Other I have list of agency permissions then want to display Agency title name on the top of the permission list.

I have table of permissions where I add column of role_category_id which is related to other table of role_categories

permissions table

Table of role_categories

role_categories table

My controller code is where I use join of two tables

$permission = RoleCatgory::Leftjoin("permissions","role_categories.id","=","permissions.role_category_id")
    ->get();

My view code

@foreach($permission as $value)
  <h3>{{ $value->category_name }}</h3>
    @if(in_array($value['id'],$rolePermissions))
          @php
              $checked="checked";
           @endphp
    @else
           @php
              $checked="";
           @endphp
    @endif
        <div class="col-md-4 mb-2">
           <input type="checkbox" class="form-check-input" id="exampleCheck" name="permission[]" @php echo $checked; @endphp value="{{ $value->id }}">
           label class="form-check-label" for="exampleCheck">{{ $value->name }}</label>
        </div>
@endforeach

Result is: Property title name is displayed on each permissions name but want to display only one time on property permission list

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

双手揣兜 2025-02-20 15:15:53

在您的终端中运行此工匠命令

php手工制作:迁移craete_roles_ and_permissions_table
然后将以下代码添加到该迁移:

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CraeteRolesAndPermissionsTable extends Migration
{

    public function up()
    {
        Schema::create('roles', function (Blueprint $table) {
            $table->increments('id', true);
            $table->string('category_name');
            $table->timestamps();
        });
        Schema::create('permissions', function (Blueprint $table) {
            $table->increments('id', true);
            $table->string('name');
            $table->string('gaurd_name');
            $table->timestamps();
        });
        Schema::create('permission_role', function (Blueprint $table) {
            $table->integer('permission_id')->unsigned();
            $table->integer('role_id')->unsigned();
            $table->foreign('permission_id')->references('id')->on('permissions')->onDelete('cascade')->onUpdate('cascade');
            $table->foreign('role_id')->references('id')->on('roles')->onDelete('cascade')->onUpdate('cascade');
            $table->primary(['permission_id','role_id']);
        });
    }

    public function down()
    {

        Schema::dropIfExists('permission_role');
        Schema::dropIfExists('permissions');
    }
}

创建一个模型为 remo App \ Models \ crom 类中的方法:

public function permissions()
{
    return $this->belongsToMany(Permission::class);
}

创建模型为许可,然后将此方法添加到 app \ models \允许类:

public function roles()
{
    return $this->belongsToMany(Role::class);
}

在您的控制器中,将$ coole变量传递到视图中:

//somthing like this
$role = Role::first() //get the first role in your database for example
return view('viewname',compact('role'))

最后,您可以在刀片中使用此关系:

<h3>{{$role->category_name}}</h3>
@foreach($role->permissions as $permission)
  <p>{{$permission->name}}</p>
@endforeach

如果您不熟悉这些概念,请阅读 laravel docs

run this artisan command in your terminal
php artisan make:migration craete_roles_and_permissions_table
then add the following code to that migration:

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CraeteRolesAndPermissionsTable extends Migration
{

    public function up()
    {
        Schema::create('roles', function (Blueprint $table) {
            $table->increments('id', true);
            $table->string('category_name');
            $table->timestamps();
        });
        Schema::create('permissions', function (Blueprint $table) {
            $table->increments('id', true);
            $table->string('name');
            $table->string('gaurd_name');
            $table->timestamps();
        });
        Schema::create('permission_role', function (Blueprint $table) {
            $table->integer('permission_id')->unsigned();
            $table->integer('role_id')->unsigned();
            $table->foreign('permission_id')->references('id')->on('permissions')->onDelete('cascade')->onUpdate('cascade');
            $table->foreign('role_id')->references('id')->on('roles')->onDelete('cascade')->onUpdate('cascade');
            $table->primary(['permission_id','role_id']);
        });
    }

    public function down()
    {

        Schema::dropIfExists('permission_role');
        Schema::dropIfExists('permissions');
    }
}

create a Model as Role and add this method in App\Models\Role class:

public function permissions()
{
    return $this->belongsToMany(Permission::class);
}

create a Model as Permission and add this method in App\Models\Permission class:

public function roles()
{
    return $this->belongsToMany(Role::class);
}

in your controller pass the $role variable into the view :

//somthing like this
$role = Role::first() //get the first role in your database for example
return view('viewname',compact('role'))

finally, you can use this Relation in your blade:

<h3>{{$role->category_name}}</h3>
@foreach($role->permissions as $permission)
  <p>{{$permission->name}}</p>
@endforeach

If you are not familiar with these concepts please read laravel docs.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文