Created
April 13, 2015 17:56
-
-
Save tomsseisums/8023303fe55706c2915d to your computer and use it in GitHub Desktop.
Laravel time scopes
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
| class Scopes extends Eloquent | |
| { | |
| public function scopeWeek($query) | |
| { | |
| return $this->scopeTimeFrame($query, 'last week midnight'); | |
| } | |
| public function scopeMonth($query) | |
| { | |
| return $this->scopeTimeFrame($query, 'last month midnight'); | |
| } | |
| public function scopeQuarter($query) | |
| { | |
| return $this->scopeTimeFrame($query, '4 months ago midnight'); | |
| } | |
| public function scopeYear($query) | |
| { | |
| return $this->scopeTimeFrame($query, 'last year midnight'); | |
| } | |
| public function scopeTimeFrame($query, $time) | |
| { | |
| $date = Carbon\Carbon::parse($time); | |
| return $query->where('created_at', '>=', $date); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment