Photo of Christoph Rumpel

Customizing Laravel Nova

The latest Laravel product, Nova, has taken the community by storm. The way you can build a simple backend in like no time is astonishing. But still, every project is different, and we need to tweak Nova here and there to make it fit our needs. With this article, I want to provide some helpful tips on how to customize Laravel Nova.

Table Of Content

I will add more and more tips as I discover them.

Define Which Items to Show on the Resource Index View

The index view is where you can see all the items of the chosen resource. In the example below, you can see all my recipes.

Laravel Nova indexy view

But sometimes you do not want to show all of them to the user. In my example, I only want to list recipes from a specific category.

Luckily, there is an easy way to define what to show on the index list. Every resource extends the base Resource class. In there, you can find the indexQuery method.

// Inside /app/Nova/Resource.php
public static function indexQuery(NovaRequest $request, $query)
{
    return $query;
}

You can copy this method to your own resource class and override it. Here is what I have used only to show breakfast recipes.

// Inside /app/Nova/Recipe.php
public static function indexQuery(NovaRequest $request, $query)
{
    return $query->where('category_id', 1);
}

Just customize this query to only show what you like on the index list.

Laravel Nova indexy view

Remove Links From Relational Fields

By default, relational fields provide a link to the related item. So when you click dinner from my example, you will get to the category item dinner.

Laravel Nova indexy view

This is a cool feature, but not always what you want. For example, if you don't want to show categories as a resource, it makes sense to skip the link as well. In earlier versions of Nova, I needed to override the Vue component to remove the link. I'm glad there was a method added called viewable.

// Inside /app/Nova/Recipe.php
public function fields(Request $request)
{
    return [
        ID::make()->sortable(),
        BelongsTo::make('Category', 'category', Category::class)
            ->viewable(false),
        Text::make('name')
    ];
}

Inside my recipe resource class, I can set the category field viewable to false. This will remove the link. Now, our category field is just plain text.

Screenshot showing Nova index view with a relational field which is a link

Hide Resource From Navigation

Since we don't want to link our recipes to their belonging category, it makes sense to also hide the category recourse from the navigation. This can be done by overriding the $displayInNavigation property in the recipe resource.

// Inside /app/Nova/Recipe.php
/**
 * @var bool 
 */
public static $displayInNavigation = false;

Now, we don't see the category resource listed in the navigation anymore.

Screenshot showing that category is not listed in the navigation anymore

Let's stay in touch

Sign up for my newsletter and I will let you know about more content and new projects of mine once a month.