You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

36 lines
1.1 KiB
PHTML

2 years ago
<?php
namespace App\Scopes;
use App\Helper\Time;
trait CommonTimeScopes
{
public function scopeRangeTime($query, $column, $timeRange, $withTime = true)
{
if (!$timeRange) {
return $query;
}
$timeRange = Time::dateTimeRange($timeRange, $withTime);
return $query->whereBetween($column, [strtotime($timeRange[0]), strtotime($timeRange[1])]);
}
public function scopeOfMonth($query, $column, $month, $withTime = true)
{
if (!$month) {
return $query;
}
$firstDate = $month . '-01';
$lastDate = Time::getMonthLastDate($month);
$timeRange = Time::dateTimeRange([$firstDate, $lastDate], $withTime);
return $query->whereBetween($column, [strtotime($timeRange[0]), strtotime($timeRange[1])]);
}
public function scopeTimeOverlay($query, $rangeColumns, $timeRange)
{
return $query->whereNot(function ($q) use ($rangeColumns, $timeRange) {
$q->where($rangeColumns[1], '<', $timeRange[0])->orWhere($rangeColumns[0], '<', $timeRange[1]);
});
}
}