1

Using eloquent, I am querying two sets of data like so:

$all_perms = Permission::all(); //all permissions
$role_perms = Auth::user()->roles()->permissions; //permissions belonging to a role

$role_perms is a subset of $all_perms and what I want is to loop both arrays and come out with a new array containing all permissions already assigned to a role together with those not yet assigned to a role.

What I have done is loop through both arrays in a foreach loop and if any one array belongs to both sets, I mark it by adding a check key with corresponding value 1 to the array so that I can identify is as a permission already assigned to a role.

foreach ($role_perms as $role_perm) {
    foreach ($all_perms as $key => $value ) {
        if (array_diff_assoc($all_perm, $role_perm)) {
            $all_perm['check'] = 1; 
        }
    }
}

but it keeps throwing the error:

array_diff_assoc(): Argument #1 is not an array

Are they better ways of doing this? Or what can I do on this one to make it work?

Thanks for any help

0

2 Answers 2

2

That's because it's a collection, not an array. If you want to get an array, try to use toArray():

$all_perms = Permission::all()->toArray();

Also, is this a typo here:

array_diff_assoc($all_perm, $role_perm);

It should be $all_perms

Sign up to request clarification or add additional context in comments.

Comments

0

Try using the wonderful contains method that is available on all your collections:

foreach ($role_perms as $role_perm) {
   if($all_perms->contains($role_perm))
   {
      // do whatever is needed
   }
}

Checkout the docs for help with the contains method.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.