Mario0
Mario0

Reputation: 33

Laravel Entrust, how to attach existing permission to role

Here is my code I am using, it runs fine but when I'm inside the database checking if everything went okay I get this result

Why am I getting 0 for the permission ID and Role ID?

$developer = new Role();
$developer->name = 'Test';

$view_customers = new Permission();
$view_customers->name = 'view_customers';
$view_customers->display_name = 'View Customers';

$developer->attachPermission($view_customers->id);

Upvotes: 2

Views: 1780

Answers (2)

Julia Shestakova
Julia Shestakova

Reputation: 899

attachPermission() works by model or array, if you want atach or detach by id use this

$developer->perms()->attach($view_customers->id);

Upvotes: 0

PamanBeruang
PamanBeruang

Reputation: 1589

try this

$developer = Role::where('name','=','Test')->get->first();

//if you want to attach new permission
$view_customers = new Permission();
$view_customers->name = 'view_customers';
$view_customers->display_name = 'View Customers';

//if you want to attach existing permission ex. you already have 'view_customers' in permission table 
$view_customers = Permission::where('name','=','view_customers')->get->first();

$developer->attachPermission($view_customers);

Upvotes: 2

Related Questions