laravel - 后期的 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');
解决方案
我使用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',
];
}
推荐阅读
- snowflake-cloud-data-platform - 如何在 Snowpark 中通过 TableFunction 使用分区
- javascript - 如何使用 IF 逻辑来获取特定值
- r - 使用十六进制代码更改 RMarkdown PDF (R) 中的文本颜色
- php - 如何使用 Jenssegers Mongodb 获取索引
- amazon-redshift - Quick Sight 在新列中按月聚合日期
- c# - 以编程方式将视图添加到 Xamarin.Android C# 中的 GridLayout
- selenium - Python - Selenium Webdriverwait NoSuchElementException 不会被 except 捕获
- c - C中的三角函数
- r - 根据条件向一列添加值
- haskell - 如何将具有多个参数的数据中的一个参数与其他数据匹配?