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);
 }