laravel / nova-issues

553 stars 34 forks source link

Partition Metrics - Access nested JSON Field #6498

Open bkhkcm opened 2 months ago

bkhkcm commented 2 months ago

Description:

In my partition metric I want to access a nested JSON field.

public function calculate(NovaRequest $request) {
  return $this->count($request, User::class, 'meta->notifications->newsletters');
}

However, I don't get any correct data back.

Screenshot 2024-08-13 at 13 55 50

If I make a query directly to the database:

SELECT JSON_UNQUOTE(JSON_EXTRACT(meta, '$.notifications.newsletters')) AS opt, COUNT(*) as aggregate FROM users GROUP BY opt;

Do I get the following data:

opt aggregate
true 58
false 13
NULL 1
bkhkcm commented 2 months ago

It seems that in the file /vendor/laravel/nova/src/Metrics/Partition.php, when calling the function formatAggregateResult, an empty string is passed for $key.