Hi,
Realise this is a little old, but I found this whilst attempting to do the same thing. I eventually found a way around it, so thought I might post it for fear it is useful to someone.
When you instantiate the metric cards with parameter it works fine. I have a big list of units, which can be classified into "types", which are subdivided into "categories", I wanted a metric for each of the types by category.
So I the type name and an id as parameters, and that works fine to display the name. In my resource file I can:
public function cards(Request $request)
{
$cards = [];
$types = \App\Models\Type::get();
foreach($types as $type) {
$cards[] = new UnitsPerCategory($type->id, $type->name);
}
return $cards;
}
Then in the constructor of the UnitsPerCategory metric, I can pick those up, and use the name to display the name for the metric, and it works fine.
function __construct($type_id, $type_name) {
parent::__construct();
$this->type_id = $type_id;
$this->type_name = $type_name;
}
public function name()
{
return str::plural($this->type_name);
}
public function calculate(NovaRequest $request)
{
//$this->type_id = $request->get('type');
return $this->count($request, \App\Models\Unit::where('type_id', $this->type_id), 'category_id')
->label(function ($value) {
switch ($value) {
case null:
return '';
default:
return \App\Models\UnitCategory::find($value)->name;
}
});
The issue I had was that the metric itself returned the same result for all of the cards, like the user above.
After a bit of messing, I realised that the data to populate the card is actually being supplied in a seperate XHR request to the server. The same XHR was request was being made for each of the cards. The solution was simply to override the URL to be used for the metric.
/**
* Get the URI key for the metric.
*
* @return string
*/
public function uriKey()
{
return 'units-per-category-' . $this->type_id;
}
And it seems to work just fine! Ok, it would have been nicer to pass the id as a param, rather than changing the URL. I tried 'units-per-category?type=' . $this->type_id; This worked to make the XHRs include the parameter, but got 404 responses, obviously the URL key is being used to setup the routing dynamically.
It's not ideal, purists will of course tell me you should not change the url like this to put a parameter in there, and indeed that you should not have an id in the URL at all, but hey, it works.
Hope it might be useful to someone!