Jordan D
Jordan D

Reputation: 728

Laravel Nova show different label based on user level

I know about the laravel label method that can be used to change the name of the resource in the nav.

I can, for instance, use the below on my Group resource to display 'Group' instead of 'Groups'.

public static function label() {

    return 'Group';

}

Does anyone know how I could define the label that's shown based on something like the user level?

what I want is something that looks like this;

public static function label() {

    if($user->level == 'Admin') {

        return 'Groups';

    }

    else {

        return 'Group';

    }

}

How do I get an instance of the user that I can use inside the function?

Upvotes: 0

Views: 452

Answers (1)

Razvan Aurariu
Razvan Aurariu

Reputation: 41

What you can do is use auth()->user() to get the current authenticated level. So your function might look something like this:

public static function label()
{
    $user = auth()->user();
    if($user->level == 'Admin') {
        return 'Groups';
    } else {
        return 'Group';
    }
}

You should try to see if there is any case where this fails if the user is not authenticated. You might fix that with some check like this:

    if(auth()->guest()) {
        return 'Group';
    }

Keep in mind that this will also change the title of the resource page.

Upvotes: 1

Related Questions