Scott Robinson Technical Lead

Laravel Nova - Trimmed Field Pattern

In a Laravel Nova based project I had a list of domains that
sometimes grew to take up most of the row it was in. I discovered the following
pattern to show a limited field on the index view and a full field on the edit
view using a combination of onlyOnIndex() and hideFromIndex():

Text::make('Domains', 'additional_domains')
    ->onlyOnIndex()
    ->displayUsing(function ($value) {
        return str_limit($value, '100', '...');
    })
    ->withMeta(['extraAttributes' => [
        'readonly' => true
    ]]),

and then a second field that is hidden from the index using hideFromIndex():

Text::make('SSL Domains', 'ssl_additional_domains')
   ->hideFromIndex()
   ->withMeta(['extraAttributes' => [
       'readonly' => true
   ]]),

The fields are automatically populated by the system and are shown read-only to
the user so this really helped to tidy up the UI. Hopefully this helps you out
if you need to limit a field you’re displaying when using Laravel Nova.

Found this useful? Everything here is free and stays that way. If it saved you an afternoon, you can buy me a coffee.