skip to Main Content

i want to a role permission list (https://phpout.com/wp-content/uploads/2023/11/z4pne.png) image but i can’t set it i have code my table structure and i actually my code is looking like this (https://phpout.com/wp-content/uploads/2023/11/ADku7.png)

my code look like this

<table class="table">
      <thead>
          <tr>
            <th scope="col">Module</th>
            <th scope="col">Show</th>
            <th scope="col">Create</th>
            <th scope="col">Edit</th>
            <th scope="col">Delete</th>
        </tr>
    </thead>
    <tbody>
        @foreach ($permissions as $k => $permission)
           <tr>
               @foreach ($permission->roles as $role)
                     <td>{{ $role->name }}</td>
              @endforeach
               <td>
                     <div class="form-check form-switch">
                      <input class="form-check-input" name="permission[]" type="checkbox" role="switch" value="{{ $permission->id }}" {{ in_array($permission->id, $rolePermissions) ? 'checked' : '' }}>
                    </div>
               </td>
           </tr>
      @endforeach
 </tbody>

i want code like enter image description here so how can set this

2

Answers


  1. if you are using spatie package for permission then I will provide you the similar structure of mine, difference will of may be styling but the overall idea will be exactly like what I am answering.

    You will first input the role name and then permissions for each module which will be in your application, create those in seeders so that evertime you don’t have to create manually.

    the blade file should look like this, as I used components if you are not used to that use simple input fields.

    <form class="form w-100" action="{{ route('settings.roles.store') }}" method="POST" enctype="multipart/form-data">
        @csrf
        <div class="fv-row mb-5">
            <x-label for="role_name" class="required">Role Name</x-label>
            <x-input class="form-control-solid {{ $errors->has('role_name') ? 'is-invalid' : '' }}"
                        id="role_name" name="role_name" value="{{ old('role_name') }}" placeholder="Role Name" autofocus required />
            @error('role_name')
                <x-error>{{ $message }}</x-error>
            @enderror
        </div>
        <div class="fv-row">
            <label class="fs-5 fw-bold form-label mb-2 mt-3">Role Permissions</label>
            <div class="table-responsive">
                <table class="table align-middle table-row-bordered fs-6 gy-5">
                    <tbody class="text-gray-600 fw-semibold">
                        <tr>
                            <td class="text-gray-800">Module 1</td>
                            <td>
                                <div class="d-flex">
                                    <label class="form-check form-check-sm form-check-custom form-check-solid me-5 me-lg-20">
                                        <x-checkbox class="h-30px w-30px" name="permissions[]" value="module1.view">
                                            <span class="form-check-label">View</span>
                                        </x-checkbox>
                                    </label>
                                    <label class="form-check form-check-sm form-check-custom form-check-solid me-5 me-lg-20">
                                        <x-checkbox class="h-30px w-30px" name="permissions[]" value="module1.create">
                                            <span class="form-check-label">Create</span>
                                        </x-checkbox>
                                    </label>
                                    <label class="form-check form-check-sm form-check-custom form-check-solid me-5 me-lg-20">
                                        <x-checkbox class="h-30px w-30px" name="permissions[]" value="module1.update">
                                            <span class="form-check-label">Edit</span>
                                        </x-checkbox>
                                    </label>
                                    <label class="form-check form-check-sm form-check-custom form-check-solid me-5 me-lg-20">
                                        <x-checkbox class="h-30px w-30px" name="permissions[]" value="module1.delete">
                                            <span class="form-check-label">Delete</span>
                                        </x-checkbox>
                                    </label>
                                </div>
                            </td>
                            {{-- Other Modules --}}
    
                        </tr>
                    </tbody>
                </table>
            </div>
        </div>
        <x-button class="btn-primary">Save Role and Permissions</x-button>
    </form>
    

    and then in you controller save this record like this,

    $request->validate([
        'role_name' => [
            'required',
            'string',
            'max:191',
            Rule::unique('roles', 'name')
        ]
    ]);
    $output = false;
    try {
        DB::beginTransaction();
    
        $role = Role::create([
            'name' => $request->input('role_name'),
        ]);
    
        $permissionService->createPermissionIfNotExists($request->input('permissions'));
    
        $role->syncPermissions($request->input('permissions'));
    
        DB::commit();
        $output = true;
    } catch (Exception|error $error) {
        DB::rollBack();
    }
    

    Create a permission servie called permission service in App/Services where you will define a function as i used, this step is not nesseary by the way.

    /**
     * Creates new permission if doesn't exist
     *
     * @param  array  $permissions
     * @return void
     */
    final public function createPermissionIfNotExists(array $permissions): void
    {
        $exising_permissions = Permission::whereIn('name', $permissions)
            ->pluck('name')
            ->toArray();
    
        $non_existing_permissions = array_diff($permissions, $exising_permissions);
    
        if (!empty($non_existing_permissions)) {
            foreach ($non_existing_permissions as $new_permission) {
                Permission::create([
                    'name' => $new_permission,
                    'guard_name' => 'web'
                ]);
            }
        }
    }
    

    and then you will be checking permission in the basis of roles, for example,

    if (! auth()->user()->can('module1.create')) {
        abort(403, 'Unauthorized action.');
    }
    
    Login or Signup to reply.
  2. You can first fetch roles with their permissions in your controller like this:

    // Pass $roles variable in your view
    $roles = Role::with('permissions')->get();
    

    And In your blade:

    <table class="table">
      <thead>
          <tr>
            <th scope="col">Module</th>
            <th scope="col">Show</th>
            <th scope="col">Create</th>
            <th scope="col">Edit</th>
            <th scope="col">Delete</th>
        </tr>
    </thead>
    <tbody>
        @foreach ($roles as $k => $role)
           <tr>
                <td>{{ $role->name }}</td>
                @foreach($role->permissions as $permission)
                   <td>
                        <div class="form-check form-switch">
                            <input class="form-check-input" name="permission[]" type="checkbox" role="switch" value="{{ $permission->id }}">
                        </div>
                   </td>
                @endforeach
           </tr>
      @endforeach
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search