laravel - 使用范围的 Laravel 集合过滤器
问题描述
我有下表:
积极的
id 开始_at 结束_at
我想每天获取所有活动,比较两个日期 starts_at 和 ends_at 并以天为单位获取差异,如下例所示:
Route::get('test', function(){
$dailyActives = \App\Models\Active::all()->filter(function ($active) {
return $active->starts_at->diffInDays($active->ends_at) >= 1 && $active->starts_at->diffInDays($active->ends_at) <= 3;
});
dd($dailyActives);
});
它100%有效。
但我想重用这段代码,因为我有更多的模式,比如每日、每周、每月。
我的想法是在模型中创建一个范围,但我不能使用过滤器,因为 $query 不是一个集合。我尝试了以下代码:
/**
* Scope a query to only include daily actives.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeDaily($query)
{
$query->filter(function ($active) {
if($active->starts_at->diffInDays($active->ends_at) >= 1 && $active->starts_at->diffInDays($active->ends_at) <=3) {
return true;
}
});
}
那么有人可以推荐我最好的方法吗?也许使用范围以及如何使用?或创建一个可重用的类,例如,只需调用 Active::daily()->get() 我每天都会获得所有活动。
提前谢谢了!
解决方案
您不需要使用过滤器。像这样使用范围$dailyActives = \App\Models\Active::interval('weekly')->get();
public function scopeInterval($query, $interval = 'daily')
{
// daily
$dateBetween = [now()->startOfDay(), now()->endOfDay()];
if($interval === 'weekly'){
$dateBetween = [now()->startOfWeek(), now()->endOfWeek()];
}
elseif($interval === 'month'){
$dateBetween = [now()->startOfMonth(), now()->endOfMonth()];
}
$query->whereBetween('created_at', $dateBetween);
return $query;
}
推荐阅读
- javascript - 如何使用 puppeteer 获取 oauth 访问令牌?
- javascript - 在 Three.js 中将 glTF 对象设置到不同层时遇到问题
- sql - T-SQL 改变字符串
- javascript - 如何获取路径未知的嵌套对象的值?
- ruby-on-rails - 如何从 Rails 应用程序连接到远程 mysql 服务器?
- sql - SQL:与平均值的百分比偏差
- python - CSV 文件 matplotlib.pyplot 绘图错误
- javascript - 是否可以使用 CSS 隐藏空行?
我刚开始学习 JavaScript 和 HTML5 的基础知识。我正在尝试创建一个食谱应用程序,用户可以在其中添加食谱。这些配方存储在一个数组中,并通过一个带有 for 循环的表循环。用户还可以编辑表格。主要功能之一应该是当表格的一行为空时,它会自动被删除。我尝试
display: none
与 CSS 一起使用,但它不起作用。这是我的表格 CSS: