Laravel 后期的 Nova 价值指标

Laravel Nova Value Metric For Later Days

Laravel Nova 建议 Value Metrics 范围是从前几天到今天。当我们使用 created_at 作为默认 date 列来显示结果时,没关系。
但是,有时我们需要根据 date 列显示结果,该列能够包含以后几天的值。例如,我们可能需要根据可能是明天的 settlement_date 来计算资源的 sum amount

    /**
     * Calculate the value of the metric.
     *
     * @param NovaRequest $request
     * @return mixed
     */
    public function calculate(NovaRequest $request)
    {
        return $this->sum(
            $request,
            Settlement::where('status', 'PENDING'), 'amount', 'settlement_date')->format('0,0');
    }

    /**
     * Get the ranges available for the metric.
     *
     * @return array
     */
    public function ranges()
    {
        return [
            'TODAY' => 'Today',
            7 => 'Week',
            30 => '30 Days',
            60 => '60 Days',
            365 => '365 Days',
        ];
    }

WHAT IF,我想知道此查询对以后几天(如明天)的价值。 类似这个查询的东西不起作用,知道吗?

return $this->sum(
            $request,
            Settlement::where('status', 'PENDING')->where('settlement_date', Carbon::tomorrow()), 'amount', 'settlement_date')->format('0,0');

我用 Trends Metric 解决了它,像这样:

    /**
     * Calculate the value of the metric.
     *
     * @param  \Laravel\Nova\Http\Requests\NovaRequest  $request
     * @return mixed
     */
    public function calculate(NovaRequest $request)
    {
        $settlement  = new \App\Models\VmsSettlement\Settlement;

        $twoDaysAgoSum      = $settlement->where('status', 'PENDING')->where('settlement_date', Carbon::today()->subDays(2)->toDateString())->sum('amount');
        $yesterdaySum       = $settlement->where('status', 'PENDING')->where('settlement_date', Carbon::today()->subDay()->toDateString())->sum('amount');
        $todaySum           = $settlement->where('status', 'PENDING')->where('settlement_date', Carbon::today()->toDateString())->sum('amount');
        $tomorrowSum        = $settlement->where('status', 'PENDING')->where('settlement_date', Carbon::today()->addDay()->toDateString())->sum('amount');
        $twoDaysLaterSum    = $settlement->where('status', 'PENDING')->where('settlement_date', Carbon::today()->addDays(2)->toDateString())->sum('amount');

        return (new TrendResult)->trend([
             Carbon::today()->subDays(2)->toDateString() . ' (2 days ago)'   => $twoDaysAgoSum,
             Carbon::today()->subDay()->toDateString() . ' (Yesterday)'            => $yesterdaySum,
             Carbon::today()->toDateString() . ' (Today)'                          => $todaySum,
             Carbon::today()->addDay()->toDateString() . ' (Tomorrow)'             => $tomorrowSum,
             Carbon::today()->addDays(2)->toDateString() . ' (2 days later)' => $twoDaysLaterSum,
        ])->format('0,0');
    }

    /**
     * Get the ranges available for the metric.
     *
     * @return array
     */
    public function ranges()
    {
        return [
            //30 => '30 Days',
            //60 => '60 Days',
            //90 => '90 Days',
        ];
    }