Last active
April 7, 2023 18:04
-
-
Save whoisthisstud/a457ff52539651af113bdddf22054d6e to your computer and use it in GitHub Desktop.
Laravel Custom Model Period Metrics
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\Http\Livewire\Dashboards; | |
| use Livewire\Component; | |
| class AdminDashboard extends Component | |
| { | |
| public $period = 'Year'; // default: Year. options: Today, Week, Month, Quarter, Year, 'All Time' | |
| protected function getMetrics($model) : array | |
| { | |
| if( is_null($model) ) { | |
| return [ | |
| 'total' => 0, | |
| 'change' => 0, | |
| 'percentage' => 0, | |
| ]; | |
| } | |
| $class = "\App\Models\\$model"; | |
| $query = $class::query(); | |
| $prevQuery = $class::query(); | |
| switch ($this->period) { | |
| case 'Today': | |
| $total = $query->today()->count(); | |
| $prevTotal = $prevQuery->yesterday()->count(); | |
| break; | |
| case 'Week': | |
| $total = $query->thisWeek()->count(); | |
| $prevTotal = $prevQuery->previousWeek()->count(); | |
| break; | |
| case 'Month': | |
| $total = $query->thisMonth()->count(); | |
| $prevTotal = $prevQuery->previousMonth()->count(); | |
| break; | |
| case 'Quarter': | |
| $total = $query->thisQuarter()->count(); | |
| $prevTotal = $prevQuery->previousQuarter()->count(); | |
| break; | |
| case 'Year': | |
| $total = $query->thisYear()->count(); | |
| $prevTotal = $prevQuery->previousYear()->count(); | |
| break; | |
| default: | |
| $total = $query->count(); | |
| $prevTotal = 0; | |
| break; | |
| } | |
| $change = $total - $prevTotal; // period over period | |
| $percentage = ($prevTotal > 0) | |
| ? round(($total / $prevTotal) * 100) | |
| : ($total > 0 | |
| ? 100 | |
| : 0); | |
| return [ | |
| 'total' => $total, | |
| 'change' => $change, | |
| 'percentage' => $percentage, | |
| ]; | |
| } | |
| public function render() | |
| { | |
| return view('livewire.dashboards.admin-dashboard') | |
| ->with([ | |
| 'userMetrics' => $this->getMetrics('User'), | |
| 'providerMetrics' => $this->getMetrics('Provider'), | |
| ]); | |
| } | |
| } |
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\Scopes; | |
| use Carbon\Carbon; | |
| trait PeriodScopes { | |
| /** | |
| * Scope a query to include records created today. | |
| * | |
| * @param \Illuminate\Database\Eloquent\Builder $query | |
| * @return \Illuminate\Database\Eloquent\Builder | |
| */ | |
| public function scopeToday($query) | |
| { | |
| $startOfDay = now()->startOfDay(); | |
| return $query->whereBetween('created_at', [$startOfDay, now()]); | |
| } | |
| /** | |
| * Scope a query to include records created yesterday. | |
| * | |
| * @param \Illuminate\Database\Eloquent\Builder $query | |
| * @return \Illuminate\Database\Eloquent\Builder | |
| */ | |
| public function scopeYesterday($query) | |
| { | |
| $startOfYesterday = now()->subDay()->startOfDay(); | |
| $endOfYesterday = now()->subDay()->endOfDay(); | |
| return $query->whereBetween('created_at', [$startOfYesterday, $endOfYesterday]); | |
| } | |
| /** | |
| * Scope a query to include records created this week. | |
| * | |
| * @param \Illuminate\Database\Eloquent\Builder $query | |
| * @return \Illuminate\Database\Eloquent\Builder | |
| */ | |
| public function scopeThisWeek($query) | |
| { | |
| $startOfWeek = now()->startOfWeek(Carbon::SUNDAY)->startOfDay(); | |
| return $query->whereBetween('created_at', [$startOfWeek, now()]); | |
| } | |
| /** | |
| * Scope a query to include records created last week. | |
| * | |
| * @param \Illuminate\Database\Eloquent\Builder $query | |
| * @return \Illuminate\Database\Eloquent\Builder | |
| */ | |
| public function scopePreviousWeek($query) | |
| { | |
| $startOfWeek = now()->subDays(7)->startOfWeek(Carbon::SUNDAY)->startOfDay(); | |
| $endOfWeek = now()->subDays(7)->endOfWeek(Carbon::SATURDAY)->endOfDay(); | |
| return $query->whereBetween('created_at', [$startOfWeek, $endOfWeek]); | |
| } | |
| /** | |
| * Scope a query to include records created this month. | |
| * | |
| * @param \Illuminate\Database\Eloquent\Builder $query | |
| * @return \Illuminate\Database\Eloquent\Builder | |
| */ | |
| public function scopeThisMonth($query) | |
| { | |
| $startOfMonth = now()->startOfMonth()->startOfDay(); | |
| return $query->whereBetween('created_at', [$startOfMonth, now()]); | |
| } | |
| /** | |
| * Scope a query to include records created last month. | |
| * | |
| * @param \Illuminate\Database\Eloquent\Builder $query | |
| * @return \Illuminate\Database\Eloquent\Builder | |
| */ | |
| public function scopePreviousMonth($query) | |
| { | |
| $startOfMonth = now()->startOfMonth()->subMonthsNoOverflow()->startOfDay(); | |
| $endOfMonth = now()->startOfMonth()->subMonthsNoOverflow()->endOfMonth()->endOfDay(); | |
| return $query->whereBetween('created_at', [$startOfMonth, $endOfMonth]); | |
| } | |
| /** | |
| * Scope a query to include records created this quarter. | |
| * | |
| * @param \Illuminate\Database\Eloquent\Builder $query | |
| * @return \Illuminate\Database\Eloquent\Builder | |
| */ | |
| public function scopeThisQuarter($query) | |
| { | |
| $startOfQuarter = now()->firstOfQuarter()->startOfDay(); | |
| return $query->whereBetween('created_at', [$startOfQuarter, now()]); | |
| } | |
| /** | |
| * Scope a query to include records created last quarter. | |
| * | |
| * @param \Illuminate\Database\Eloquent\Builder $query | |
| * @return \Illuminate\Database\Eloquent\Builder | |
| */ | |
| public function scopePreviousQuarter($query) | |
| { | |
| $startOfQuarter = now()->firstOfQuarter()->subMonthsNoOverflow(3)->startOfDay(); | |
| $endOfQuarter = now()->firstOfQuarter()->subMonthsNoOverflow(3)->lastOfQuarter()->endOfDay(); | |
| return $query->whereBetween('created_at', [$startOfQuarter, $endOfQuarter]); | |
| } | |
| /** | |
| * Scope a query to include records created this year. | |
| * | |
| * @param \Illuminate\Database\Eloquent\Builder $query | |
| * @return \Illuminate\Database\Eloquent\Builder | |
| */ | |
| public function scopeThisYear($query) | |
| { | |
| $startOfYear = now()->startOfYear()->startOfDay(); | |
| return $query->whereBetween('created_at', [$startOfYear, now()]); | |
| } | |
| /** | |
| * Scope a query to include records created last year. | |
| * | |
| * @param \Illuminate\Database\Eloquent\Builder $query | |
| * @return \Illuminate\Database\Eloquent\Builder | |
| */ | |
| public function scopePreviousYear($query) | |
| { | |
| $startOfYear = now()->startOfYear()->subDays(2)->startOfYear()->startOfDay(); | |
| $endOfYear = now()->startOfYear()->subDays(2)->endOfYear()->endOfDay(); | |
| return $query->whereBetween('created_at', [$startOfYear, $endOfYear]); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment