Undocumented Laravel Nova trick

Ouch :) This article was published 4 years ago - might be out of date, check out stuff or leave comment

Laravel nova documentation is quite exhaustive but lacks detailed explanation for many various subjects.

Tip#1 : Computed value metric card 

Resource metrics cards are very powerful helpers that allow quick viewing of summary information. But the documentation about Value Metrics configuration only mentions how to sum, average single database fields. It took me quite a while to wander around forums and issues to finally find an easy way of computing two field value, like for instance the calculated cost of hours multiplied by each hour rate.

Using DB Raw allows you to perform calculations of two or more fields, remember to "use" the DB class in the header of the class : 

namespace App\Nova\Metrics;

use Laravel\Nova\Http\Requests\NovaRequest;
use Laravel\Nova\Metrics\Value;
use Illuminate\Support\Facades\DB;
class WorkTotal extends Value
     * Calculate the value of the metric.
     * @param  \Laravel\Nova\Http\Requests\NovaRequest  $request
     * @return mixed
    public function calculate(NovaRequest $request)
        return $this->sum($request, \App\WorkTasks::class,DB::raw('h*tx'), "date_wk")->format("0,0");;


TIP #2 : search records by tag

it is a good surprise that nova intagrates perfectly , or almost perfectly with Spatie's famous laravel tag package, that migrates a full featured polymorphic Many to Many tag / model relation ship, very useful for blog structure. In my particular case I use it for tagging tasks , in association with the spatie nova tags field package which integrates the tag field in the edit form, but also in the index page with possibility of link to the tag resource.

What was missing for me is a quick integration of a filter : I added on the two  multiselect filter available from novapackages to my composer json easily, created the filter wi no big trouble, but it was less easy to figure out the where clause to apply to that specific morph to many relation. Solution here after few hours of ... rest... Brain works better when relaxed.

Tip#3 : default date on create forms

I decided to use a custom field for creation date in my table, not the basic laravel create date field. That's because I want to let the user choose whether the current date or other should be saved when creating the record. Well, was not quite that easy to specify default current time on a modifiable edit date field, but well the line below does the job

DateTime::make("date_wk")->required()->format('Y-m-d h:i:s')->withMeta(["value" =>date('Y-m-d H:i:s')])->onlyOnForms()->hideWhenUpdating(),

Tip #4 : display nested rows from HasMany relation ship on index page

found many discussions on the subject of staying within the index page while working on subrow from relationship. On nova3, you can try the plugin hasmany-to-modal, even though it is quite old I tested it and it works fine. It simply adds a column with relationship count and a link to a modal display of sub records. A quick look through the code will answer the question of how to customize the label displayed on each line next to the nested record count, for instance here a reference class that BelongsTo products:

HasmanyToModal::make('Reference Count', 'references',  \App\Nova\Reference::class)->singularLabel(''),
To top