首页 > 解决方案 > 后期的 Laravel Nova 价值指标

问题描述

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

    /**
     * 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',
        ];
    }

什么如果,我想知道这个查询在明天这样的晚些时候的值。像这样的查询不起作用,知道吗?

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

标签: laravellaravel-nova

解决方案


我使用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',
        ];
    }

推荐阅读