mardi 1 décembre 2015

When I pass a $permission into the $gate-define() closure having eager loaded it's related roles -- the roles collection is empty. Why?

I'm essentially recreating Jeffrey Way's example for defining abilities within Larave's Gate/ACL. When I pass a $permission into the $gate-define() closure having eager loaded its related roles -- the roles collection is empty. If I dd($permission) outside of the closure scope the roles are there. If I dd($permission) inside of the closure scope they have vanished. Why? Please enlighten me.

public function boot(GateContract $gate)
{
    $this->registerPolicies($gate);
    foreach ($this->getPermissions() as $permission) {
        dd($permission); // this has the roles
        $gate->define($permission->name, function($user) use($permission)  {
            dd($permission); // here the roles are gone -- the key is present but the collection is empty
            return $user->hasRole($permission->roles);
        });
    }

}

protected function getPermissions()
{
    return Permission::with('roles')->get();
}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire