mercredi 18 mai 2016

Laravel 5 checkif Admin middleware logic not correct (one to many with role)

I'm building a Laravel 5 REST-webapp. Now I would like to add an Admi. I have done so by setting an one-to-many relation (user can have 1 role, 1 role can have many users) table Like so:

public function role()
{
    return $this->hasOne(Role::class);
}

This works, I have a user with an role_id. But now I'm stuck writing the middleware and using this role_id correctly.

Now, my admin middleware is

public function handle($request,  Closure $next, $guard = null)
{
    if (Auth::User()->isRole('Admin')) {
        return $next($request);
    }
    return redirect()->guest('login');
}

And in my User Model I have:

public function isRole($roleName)
{

    if ($this->with('role') == $roleName)
    {
        print_r($this->with('role.name'));
    }

    return false;
}

But this code just sends me to the home page.

My Middleware Logic is wrong (everything else is setup correctly).

Kind regards



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/207xQjI
via IFTTT

Aucun commentaire:

Enregistrer un commentaire