Paulund

Creating A Laravel Nova Unverified Lense

In Laravel 5.7 a new feature was added to allow you to ask your users for email verification before they're allowed to access anything inside the auth middleware. This is a nice little feature to make sure that your users are real users.

With the introduction of Laravel Nova it allows you to manage different resources for your application such as users. One of the features with Laravel Nova is lenses, these allow you to create a new view for your resource.

In this tutorial we're going to create a new Nova Lense that will quickly display to you which users are unverified and the date they signed up.

Create A Lense

To create a new lense in Nova you can run the command

php artisan nova:lens UnverifiedUsers

This will create a file in app/Nova/Lenses/UnverifiedUsers.php it will already have a template for the class that you need, all we need to do is change the query for the page.

We need to search for users where the email_verified_at field is null.

public static function query(LensRequest $request, $query)
{
    return $request->withOrdering($request->withFilters(
	    $query->where('email_verified_at', null)
    ));
}

Then you can add the fields for the table.

/**
 * Get the fields available to the lens.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return array
 */
public function fields(Request $request)
{
	return [
    	ID::make('ID', 'id')->sortable(),

        Text::make('Name')
                ->sortable(),

		Text::make('Email')
                ->sortable(),

		DateTime::make('created_at'),
    ];
}

Register Your Lense

Then you need to register the lense in your User resource, by adding the class to the lenses method.

public function lenses(Request $request)
{
	return [
    	new UnverifiedUsers
	];
}

Now you can navigate to resources/users/lens/unverified-users to see all the users in your application that haven't verified their email yet.