2

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

apokryfos
  • 38,771
  • 9
  • 70
  • 114
Ahil Khan
  • 227
  • 1
  • 4
  • 12

1 Answers1

0

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.

Reza Zand
  • 346
  • 2
  • 6