Add custom method to Laravel Nova Trend (TrendResult)
Hi all:
I was wondering if it was possible to add an additional method to a Nova Trend Metric?
I would like to add modified version of showLatestValue() which is defined in Laravel\Nova\Metrics\TrendResult. I have already written the method and it works if I insert it directly into the TrendResult class.
Of course this is not ideal as it would get overridden with any Nova updates.
What would be the best way to insert this method without directly updating TrendResult?
You can create your own Trend class and return that in the response. Here is an example.
public function calculate(NovaRequest $request): MyTrendResult
{
$result = $this->countByDays($request, User::class);
return (new MyTrendResult())->trend($result->trend);
}
Basically you just do the normal call and collect the data. You can then pass the data to your own class and call the trend method. Your own class has a custom implementation of showLatestValue