jeudi 24 novembre 2016

Laravel calling a method on a user if not logged in

I have a method in my User model that is:

public function hasRole($roles)
{
    foreach ($roles as $role) {
        if ($role === $this->role_id) {
            return true;
        }
    }

    return false;
}

And I am adding a class to the body of my app by checking this methods return:

<div id="spark-app" v-cloak class="">

This correctly works, but if there is no logged in user I of course receive an error.

Call to a member function hasRole() on null

Is there a way to still use this shorthand statement along with checking if there is a user?

I could wrap the shorthand in an inline if statement but it feels messy. Is there a better way of handling this?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire