undocumented nova : tips and trick

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(),
0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *