Home > database >  laravel-permission convert many collection to single array
laravel-permission convert many collection to single array

Time:02-22

I have a problem with my foreach. I have collection data like this:

["post access", "post create", "post read", "post update", "post delete", "publish access", ...]

but I need to convert that into this:

[
   "name" => "post",
   "access" => false,
   "create" => false,
   "read" => false,
   "update" => false,
   "delete" => false
], [
   "name" => "publish",
   "access" => false
], [...]

btw I'm still on progress of learning laravel-permission, and I make that for the checkbox in the blade.

how did I make that? please help me. Thanks

CodePudding user response:

You have to use both String Helpers and Collection Methods for this.

$collection = collect(["post access", "post create", "post read", "post update", "post delete", "publish access", ...]);

$array = array();

$collection->each(function($item. $key){
    if(Str::contains(['post','get'], $item) && (! isset($array[0]['name']))){
         $array[0]['name'] = Str::before(' ', $item);
    }
    if (Str::contains(['access','create','read','update','delete'], $item){
         $name_removed = Str::remove($array[0]['name'] . ' ', $item);
         $array[0][$name_removed] = false;
    }
    if (Str::contains('publish access', $item){
         $array[1]['name'] = Str::remove(' access', $item);
         $array[1]['access'] = false;
    }
}

CodePudding user response:

ok, I'm done with that

use Illuminate\Support\Str;

$array = ["post access", "post create", "post read", "post update", "post delete", "publish access"];

foreach($array as $permission){
    $generatePermissions[] = [
        "name" => Str::remove(["access", "create", "read", "update","delete"], $permission)
    ];

    if(Str::contains($permission, "access")){
        $permissions[array_search(Str::remove(["access", "create", "read", "update", "delete"],$permission), array_column($generatePermissions, "name"))]["access"] = false;
    }elseif(Str::contains($permission, "create")){
        $permissions[array_search(Str::remove(["access", "create", "read", "update", "delete"],$permission), array_column($generatePermissions, "name"))]["create"] = false;
    }elseif(Str::contains($permission, "read")){
        $permissions[array_search(Str::remove(["access", "create", "read", "update", "delete"],$permission), array_column($generatePermissions, "name"))]["read"] = false;
    }elseif(Str::contains($permission, "update")){
        $permissions[array_search(Str::remove(["access", "create", "read", "update", "delete"],$permission), array_column($generatePermissions, "name"))]["update"] = false;
    }elseif(Str::contains($permission, "delete")){
        $permissions[array_search(Str::remove(["access", "create", "read", "update", "delete"],$permission), array_column($generatePermissions, "name"))]["delete"] = false;
    }
}

$convertedArray = array_filter($generatePermissions, function($array){
                      if(count($array) > 1){
                          return $array;
                      }
                  });

dd($convertedArray);

this is the solution I solved. thanks

  • Related