Advertisement
Guest User

Untitled

a guest
Jan 24th, 2020
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.07 KB | None | 0 0
  1. <?php
  2.  
  3.  
  4. public function createRank()
  5. {
  6.          //A user wants to add a rank with the following permission id's
  7.          $user_input_permissions = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10];
  8.          $user_input_rank_name = "Customer Support";
  9.          $canAddRank = true;
  10.          //we ned to be sure all items are numbers and unique
  11.         if(array_filter($user_input_permissions,'is_int')){
  12.            
  13.             $ids = array_unique($user_input_permissions);
  14.             //we can check using NOT IN and array instead of lopp
  15.             $permission = Permission::whereNotIn('id', $ids)->first();
  16.             if($permission)
  17.             {
  18.                 $canAddRank = false;
  19.                 break;
  20.             }
  21.         }
  22.        
  23.  
  24.         if(!$canAddRank)
  25.             return "Couldn't add rank. User tried to add a rank that didn't exist!";
  26.  
  27.         $rank = new CompanyRank;
  28.         $rank->title = $user_input_rank_name;
  29.         $rank->permissions = $user_input_permissions;
  30.         $rank->save();
  31.         return "Added Rank Successfully!";
  32.  
  33.          }
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement