0

I am trying to apply roles and permission on Laravel 5.5 with Entrust.

I used this, as the documentation inside the: App\models\Role.php

<?php namespace App;

use Zizaco\Entrust\EntrustRole;

class Role extends EntrustRole
{

    $owner = new Role();
    $owner->name         = 'owner';
    $owner->display_name = 'Project Owner'; // optional
    $owner->description  = 'User is the owner of a given project'; // optional
    $owner->save();

    $admin = new Role();
    $admin->name         = 'admin';
    $admin->display_name = 'User Administrator'; // optional
    $admin->description  = 'User is allowed to manage and edit other users'; // optional
    $admin->save();

    $manager = new Role();
    $manager->name         = 'manager';
    $manager->display_name = 'Company Manager'; // optional
    $manager->description  = 'User is a manager of a Department'; // optional
    $manager->save();
}

?>

However, when i am trying to assign role to a user I receiving this issue:

{
..
$owner = new Role();
$owner->name = 'owner';
...
}

"Parse error: syntax error, unexpected '$owner' (T_VARIABLE), expecting function (T_FUNCTION) or const (T_CONST)"

Can you advice me please?

  • where do you assign the role to the user?? – madalinivascu Nov 08 '17 at 09:43
  • Inside a Controller, with a post method - the user's email account $admin = \App\Role::find(2); $user = User::where('email', '=', 'email@domain.tld')->first(); // role attach alias $user->attachRole($admin); // parameter can be an Role object, array, or id // or eloquent's original technique $user->roles()->attach($admin->id); // id only –  Nov 08 '17 at 09:49
  • where's the owner variable in what you posted in the comment? – madalinivascu Nov 08 '17 at 09:51
  • i changed admin to owner but still same issue. however there was a warning on my IDE about this error (T_variable) before i prevent in actions. Just follow this documentation https://github.com/Zizaco/entrust. –  Nov 08 '17 at 09:59
  • provide me with the line where this error is happening – madalinivascu Nov 08 '17 at 10:01
  • https://imgur.com/a/9e530 –  Nov 08 '17 at 10:04
  • now i see it lol, you need to put the code in a function man not plug it in the model class directly, you are new to oop from what i see :)) – madalinivascu Nov 08 '17 at 10:07
  • better use a seeder class – madalinivascu Nov 08 '17 at 10:07
  • thank you very much –  Nov 08 '17 at 10:24

1 Answers1

1

How about a function/method ? ;)

<?php namespace App;

use Zizaco\Entrust\EntrustRole;

class Role extends EntrustRole
{

public function setup() 
{
    $owner = new Role();
    $owner->name         = 'owner';
    $owner->display_name = 'Project Owner'; // optional
    $owner->description  = 'User is the owner of a given project'; // optional
    $owner->save();

    $admin = new Role();
    $admin->name         = 'admin';
    $admin->display_name = 'User Administrator'; // optional
    $admin->description  = 'User is allowed to manage and edit other users'; // optional
    $admin->save();

    $manager = new Role();
    $manager->name         = 'manager';
    $manager->display_name = 'Company Manager'; // optional
    $manager->description  = 'User is a manager of a Department'; // optional
    $manager->save();
}
}
lchachurski
  • 1,770
  • 16
  • 21