Laravel 5.5 并委托

Laravel 5.5 and Entrust

我正在尝试使用 Entrust 在 Laravel 5.5 上应用角色和权限。

我用过这个,作为文档里面的: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();
}

?>

但是,当我尝试将角色分配给用户时,我收到了这个问题:

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

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

你能给我一些建议吗?

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();
}
}