[Solution] Laravel Nova login screen - “Call to a member function can() on null”

Sometimes my development environment experiences the following error when attempting to log in to Laravel Nova:

Call to a member function can() on null (View: {PROJECT_DIR}/vendor/laravel/nova/resources/views/layout.blade.php)

What causes this?

First it’s important to note that it is possible you are receiving this error because you are trying to call ->can() on a null variable you thought contained an instance of a user object. If you are sure that is not your issue then please read on.

I receive this error when I am logged into my local environment and I dump and refresh the data in my database and clear my cache. Since my browser still has a cookie for localhost it expects user-specific data about my user to be present in the cache and/or database. Since the cache and database were cleared, though, that data doesn’t exist. This set of actions is what I have determined causes the error, at least for me.

Here are some potential causes:

  1. User/developer is logged into Laravel Nova in a browser
  2. The cache and/or database are cleared via artisan commands
  3. The user/developer attempts to refresh their browser’s Nova tab but their user no longer exists, putting them in a sort of limbo.

Fix 1: Purge session data from your server

Of the two fixes offered here, this is the fastest. However, if you are wanting to preserve your localhost session data you will not want this choice.

Simply run php artisan optimize:clear to clear your configuration cache, route cache, views cache, events cache, and application cache. When you refresh your Nova login page should appear.

Fix 2: Deleting localhost cookies in-browser

  1. Go into browser cookies and clear cookies for 127.0.0.1 and localhost.

Clearing Cookies for 127.0.0.1

Clearing cookies for 127.0.0.1

Clearing Cookies for localhost

Clearing cookies for localhost

  1. After that, browser cache needs to be cleared.

Clearing Browser Cache

Clearing browser cache

  1. Refresh the page, and you will see the login page now loads properly. Nova Login

    Nova login

The Explanation

localhost is the default name of your computer, and 127.0.0.1 is the “loopback” address for your computer, which is reserved for use on local area networks. They can essentially be used interchangeably, with the main difference being that localhost does not go through the network card or firewall and 127.0.0.1 does.

Once cookies are cleared in your browser for both hosts your browser will be able to create new cookies for 127.0.0.1 and localhost, which is desired. These new cookies will allow Nova to understand that you are no longer trying to log in as the now-deleted user that the previous cookie was associated with, and you are now logging in as a fresh user.

The typical logout procedure would put the cookie into a state where this Nova route would load properly, but (as described earlier) dumping your cache and/or database can get your Laravel application into this funky state.