laravel-acl icon indicating copy to clipboard operation
laravel-acl copied to clipboard

assignRole() does not work, but roles->attach() does

Open jvalentinei opened this issue 9 years ago • 2 comments

I am attempting to set up laravel acl, and ran across an issue with assignRole().

I am running laravel 5.2 and acl 0.1.3

Here is unit test code to illustrate the issue:

$roleAdmin = new Role(); $roleAdmin->name = 'Administrator'; $roleAdmin->slug = 'administrator'; $roleAdmin->description = 'manage administration privileges'; $roleAdmin->save();

$user = User::find(1);

$user->roles()->attach($roleAdmin->id); $this->assertTrue($user->is('administrator')); // test passes

$user = User::find(2); $user->assignRole($roleAdmin); $this->assertTrue($user->is('administrator')); // test fails

Note that this includes the most recent pull request that updates HasRole::is() to use contains() rather than in_array(), otherwise it throws an exception.

Please let me know what the issue might be or if more information is needed to figure out the issue.

jvalentinei avatar Feb 24 '16 15:02 jvalentinei

I'm having this issue too. syncRoles() works, but assignRole() doesn't. 😞

nelson6e65 avatar Jun 27 '17 00:06 nelson6e65

Well... I noticed I had to re-query the user to get the updated roles:

<?php
$user = User::first();

$user->syncRoles(['dev']);
$user->assignRole('customer');

$user2 = User::first();

dd($user->getRoles(), $user2->getRoles());

Outputs:

array:1 [
  0 => "dev"
]
array:2 [
  0 => "dev"
  1 => "customer"
]

nelson6e65 avatar Jun 27 '17 01:06 nelson6e65