Skip to content

Instantly share code, notes, and snippets.

@tomsseisums
Created April 13, 2015 17:56
Show Gist options
  • Select an option

  • Save tomsseisums/8023303fe55706c2915d to your computer and use it in GitHub Desktop.

Select an option

Save tomsseisums/8023303fe55706c2915d to your computer and use it in GitHub Desktop.
Laravel time scopes
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