public function setChilds($childIds)
 {
     $currentChildIds = $this->childIds();
     $deleteIds = array_diff($currentChildIds, $childIds);
     foreach ($deleteIds as $id) {
         $delete = Resourcepermission::find($id);
         self::where('parent_id', $this->id)->where('permission_id', $delete->permission_id)->where('resource_id', $delete->resource_id)->delete();
         foreach (Role::all() as $role) {
             DB::table('lcc_resourcepermission_role')->where('role_id', $role->id)->where('parent_id', $this->id)->where('resourcepermission_id', $delete->id)->delete();
         }
     }
     $newChildIds = array_diff($childIds, $currentChildIds);
     foreach ($newChildIds as $id) {
         $child = new Resourcepermission();
         $child->permission_id = Resourcepermission::find($id)->permission_id;
         $child->resource_id = Resourcepermission::find($id)->resource_id;
         $child->parent_id = $this->id;
         $child->save();
         foreach (Role::all() as $role) {
             if ($role->resourcePermissions()->where('id', $this->id)->exists()) {
                 DB::table('lcc_resourcepermission_role')->insert(array('role_id' => $role->id, 'resourcepermission_id' => $child->id, 'parent_id' => $this->id));
             }
         }
     }
 }
 public function manageRolePermissionsAction($id)
 {
     if (Auth::user() && Auth::user()->id == Config::get('laracancan.super_admin')) {
         $role = Role::find($id);
         $role->resourcePermissions()->detach();
         foreach (Resource::all() as $resource) {
             $permissions = Input::get($resource->id . "_resourcepermissions");
             if ($permissions != null) {
                 foreach ($permissions as $permission) {
                     $resourcePermission = Resourcepermission::where('permission_id', $permission)->where('resource_id', $resource->id)->first();
                     $role->resourcePermissions()->detach([$resourcePermission->id]);
                     $role->resourcePermissions()->attach([$resourcePermission->id]);
                     foreach ($resourcePermission->childResourcePermissions as $child) {
                         $role->resourcePermissions()->attach([$child->id => ['parent_id' => $resourcePermission->id]]);
                     }
                 }
             }
         }
         return redirect()->back()->with('flash_success', 'Permissions saved successfully!');
     }
     return response(view('laracancan::master.401'), 401);
 }
                 @endforeach
             </select>
         </div>
         <div class="col-md-6">
             @foreach(\Hamedmehryar\Laracancan\Models\Resource::all() as $resource)
                 <select name="{{$resource->id}}_resourcepermissions[]" multiple style="width: 100%; display: none" size="20" id="resourcepermissions_{{$resource->id}}" class="resource-selects">
                     <?php 
$roleResourcePermissions = $role->resourcePermissions;
$roleResourcePermissionsIds = array();
foreach ($roleResourcePermissions as $p) {
    $roleResourcePermissionsIds[] = $p->id;
}
?>
                     @foreach($resource->resourcePermissions as $permission)
                         <?php 
$resourcePermission = \Hamedmehryar\Laracancan\Models\Resourcepermission::where('permission_id', $permission->id)->where('resource_id', $resource->id)->where('parent_id', null)->first();
$parents = json_encode($resourcePermission->parentsIds());
?>
                         {{$resourcePermission->id."<br>"}}

                             @if(in_array($resourcePermission->id, $roleResourcePermissionsIds))
                                <option class="resourcepermission-can-also" value="{{$permission->id}}" data-parents="{{$parents}}" selected>{{$permission->display_name}}</option>
                             @else
                                 <option class="resourcepermission-can-also" value="{{$permission->id}}" data-parents="{{$parents}}">{{$permission->display_name}}</option>
                             @endif
                     @endforeach
                 </select>
             @endforeach
         </div>
     </div>
     <hr>
 public function postCanAlso($id)
 {
     if (Auth::user() && Auth::user()->id == Config::get('laracancan.super_admin')) {
         try {
             $resourcePermission = Resourcepermission::findOrFail($id);
         } catch (ModelNotFoundException $e) {
             return redirect()->back()->with('flash_error', 'Permission not found for this resource');
         }
         $resourcePermissionIds = Input::get('resourcepermissions');
         if ($resourcePermissionIds != null) {
             $resourcePermission->setChilds($resourcePermissionIds);
         } else {
             $resourcePermission->setChilds(array());
         }
         return redirect()->back()->with('flash_success', 'Records Updated Successfully');
     }
     return response(view('laracancan::master.401'), 401);
 }