Skip to content

How to make report with grouping by days ? #68

@sergeynilov

Description

@sergeynilov

Using "kirschbaum-development/nova-chartjs": "^2.0" I want to add chart for completed orders within current month:

I created report resource with data filter in indexQuery method :

<?php

namespace App\Nova\Reports;

use App\Nova\Resource;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Laravel\Nova\Fields\ID;
use Laravel\Nova\Http\Requests\NovaRequest;
use KirschbaumDevelopment\NovaChartjs\NovaChartjs;

class CompletedOrdersByDaysChartJS extends Resource
{
    /**
     * The model the resource corresponds to.
     *
     * @var class-string<\App\Models\Order>
     */
    public static $model = \App\Models\Order::class;

    /**
     * The single value that should be used to represent the resource when being displayed.
     *
     * @var string
     */
    public static $title = 'Completed orders by days 999';

    /**
     * The columns that should be searched.
     *
     * @var array
     */
    public static $search = [
        'id',
    ];

    public static function indexQuery(NovaRequest $request, $query)
    {
        $today = Carbon::now();
        $query->onlyCompleted()->whereMonth('completed_by_manager_at', $today->month)
            ->whereYear('completed_by_manager_at', $today->year);
        return parent::indexQuery($request, $query);
    }


    /**
     * Get the fields displayed by the resource.
     *
     * @param  \Laravel\Nova\Http\Requests\NovaRequest  $request
     * @return array
     */
    public function fields(NovaRequest $request)
    {
        $chartName = 'Completed orders by days';
        return [
            NovaChartjs::make('Completed orders by month', 'novaChartjsMetricValue', function () use($chartName) {
                return optional($this->novaChartjsMetricValue()->where('chart_name', $chartName)->first())->metric_values ?? [];
            }),
        ];

    }


    public static function canUpdate(Request $request)
    {
        return false;
    }

    public static function canAdd(Request $request)
    {
        return false;
    }

    public static function canDelete(Request $request)
    {
        return false;
    }




    /**
     * Get the cards available for the request.
     *
     * @param  \Laravel\Nova\Http\Requests\NovaRequest  $request
     * @return array
     */
    public function cards(NovaRequest $request)
    {
        return [];
    }

    /**
     * Get the filters available for the resource.
     *
     * @param  \Laravel\Nova\Http\Requests\NovaRequest  $request
     * @return array
     */
    public function filters(NovaRequest $request)
    {
        return [];
    }

    /**
     * Get the lenses available for the resource.
     *
     * @param  \Laravel\Nova\Http\Requests\NovaRequest  $request
     * @return array
     */
    public function lenses(NovaRequest $request)
    {
        return [];
    }

    /**
     * Get the actions available for the resource.
     *
     * @param  \Laravel\Nova\Http\Requests\NovaRequest  $request
     * @return array
     */
    public function actions(NovaRequest $request)
    {
        return [];
    }
}

I see next page :

https://img001.prntscr.com/file/img001/y6kn43edST2EnLQVu69Aww.png

Checking sql :

   SELECT *
    FROM `orders`
    WHERE `orders`.`status` = 'O'     AND month(`completed_by_manager_at`) = '03'     AND year(`completed_by_manager_at`) = 2025
    ORDER BY `orders`.`id` desc limit 26 offset 0

and 2 rows found with field with 2025-03-01 08:46:07 and 2025-03-02 09:45:01 dates.

In the generated report there are 2 rows, but day completed_by_manager_at is not marked.

I did not understand how to show report relative to my dates ?

I suppose to make some grouping by days.

I added logging messages into getNovaChartjsSettings method of Order model, but looks like getNovaChartjsSettings method is not calling.

Not sure, if I do not have to apply any getAdditionalDatasets method in the Order model in my case?

Order Model, has no title/name fields - as I see "undefined" label...

How can I make my report ?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions