Created
February 27, 2021 14:23
-
-
Save jongravois/bd25d22b4567a23989e587efea7e3748 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Traits; | |
use Carbon\Carbon; | |
use Illuminate\Database\Eloquent\Builder; | |
trait Posted | |
{ | |
public function scopeYearlyReport(Builder $query, $year = null): Builder | |
{ | |
return $query->whereYear('post_date', $year ?? Carbon::now()->year); | |
} // end function | |
public function scopeThisYearReport(Builder $query): Builder | |
{ | |
return $query->whereYear('post_date', Carbon::now()->year); | |
} // end function | |
public function scopeLastYearReport(Builder $query): Builder | |
{ | |
return $query->whereYear('post_date', Carbon::now()->subYear()->year); | |
} // end function | |
public function scopeMonthlyReport(Builder $query, $month = null, $year = null): Builder | |
{ | |
return $query->whereYear('post_date', $year ?? Carbon::now()->year)->whereMonth('post_date', $month ?? Carbon::now()->month); | |
} // end function | |
public function scopeThisMonthReport(Builder $query): Builder | |
{ | |
return $query->whereYear('post_date', Carbon::now()->year)->whereMonth('post_date', Carbon::now()->month); | |
} // end function | |
public function scopeLastMonthReport(Builder $query): Builder | |
{ | |
return $query->whereYear('post_date', Carbon::now()->subMonth()->year)->whereMonth('post_date', Carbon::now()->subMonth()->month); | |
} // end function | |
public function scopeThisWeekReport(Builder $query): Builder | |
{ | |
return $query->whereBetween('post_date', [Carbon::now()->startOfWeek(), Carbon::now()->endOfWeek()]); | |
} // end function | |
public function scopeLastWeekReport(Builder $query): Builder | |
{ | |
return $query->whereBetween('post_date', [Carbon::now()->startOfWeek()->subWeek()->format('Y-m-d'), Carbon::now()->endOfWeek()->subWeek()->format('Y-m-d')]); | |
} // end function | |
public function scopeWeeklyReport(Builder $query, $weekNumber = null, $year = null): Builder | |
{ | |
if($year) { | |
$date = Carbon::create($year); | |
} else { | |
$date = Carbon::now(); | |
} // end if | |
if ($weekNumber) { | |
$date->setISODate(date('Y'), $weekNumber); | |
} | |
return $query->whereBetween('post_date', [ | |
$date->startOfWeek()->format('Y-m-d'), | |
$date->endOfWeek()->format('Y-m-d') | |
]); | |
} // end function | |
public function scopeDailyReport(Builder $query, $date = null): Builder | |
{ | |
return $query->whereDate('post_date', $date ?? Carbon::today()); | |
} // end function | |
public function scopeTodayReport(Builder $query): Builder | |
{ | |
return $query->whereDate('post_date', Carbon::today()); | |
} // end function | |
public function scopeYesterdayReport(Builder $query): Builder | |
{ | |
return $query->whereDate('post_date', Carbon::yesterday()); | |
} // end function | |
public function scopeHourlyReport(Builder $query, $from = null, $to = null, $date = null): Builder | |
{ | |
return $query->whereDate('post_date', $date ?? Carbon::today())->whereTime('post_date', '>', $from ?? Carbon::now()->subHour())->whereTime('post_date', '<=', $to ?? Carbon::now()); | |
} // end function | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment