Date Scopes in Laravel (10)
In Laravel, date scopes allow you to define reusable methods on your model that can be used to query records based on a specific date range. For example, you can define a scope to retrieve all records that were created between a given start and end date.
Here's an example of how to define a date scope in Laravel:
class Post extends Model
{
public function scopeCreatedAtBetween($query, $start, $end)
{
return $query->whereBetween('created_at', [$start, $end]);
}
}
In this example, we define a scope named createdAtBetween
that takes two arguments: a start date and an end date. The scope uses the whereBetween
method to compare the created_at
column against the given start and end dates.
You can use this scope like so:
$posts = Post::createdAtBetween('2022-01-01', '2022-01-31')->get();
This will retrieve all posts that were created between January 1st and January 31st, 2022.
You can define other date scopes using similar techniques. For example, to retrieve records that were updated between a given start and end date, you can create a scope like this:
class Post extends Model
{
public function scopeUpdatedAtBetween($query, $start, $end)
{
return $query->whereBetween('updated_at', [$start, $end]);
}
}
This will retrieve all posts that were updated between January 1st and January 31st, 2022.
Let's say we want to get the records for the last 12 hours.
To retrieve records that were created or updated in the last 12 hours, you can modify the date scope method to use the where
method with a comparison operator like so:
class Post extends Model
{
public function scopeCreatedAtLast12Hours($query)
{
return $query->where('created_at', '>=', now()->subHours(12));
}
public function scopeUpdatedAtLast12Hours($query)
{
return $query->where('updated_at', '>=', now()->subHours(12));
}
}
If we want to get the records for the last week:
class Post extends Model
{
public function scopeCreatedAtLastWeek($query)
{
return $query->where('created_at', '>=', now()->subWeek());
}
public function scopeUpdatedAtLastWeek($query)
{
return $query->where('updated_at', '>=', now()->subWeek());
}
}
If we want to get the records for the last month:
class Post extends Model
{
public function scopeCreatedAtLastMonth($query)
{
return $query->where('created_at', '>=', now()->subMonth());
}
public function scopeUpdatedAtLastMonth($query)
{
return $query->where('updated_at', '>=', now()->subMonth());
}
}
And to retrieve it:
$posts = Post::createdAtLastMonth()->get();
$updatedPosts = Post::updatedAtLastMonth()->get();
his will retrieve all posts that were created or updated in the last 1 month. Note that you can modify the subMonth
method to any other time interval like subDays
or subYears
depending on the requirements.
Finally for the last one year:
class Post extends Model
{
public function scopeCreatedAtLastYear($query)
{
return $query->where('created_at', '>=', now()->subYear());
}
public function scopeUpdatedAtLastYear($query)
{
return $query->where('updated_at', '>=', now()->subYear());
}
}
In this example, we define two new scopes createdAtLastYear
and updatedAtLastYear
that don't take any arguments. Instead, they use the where
method to compare the created_at
or updated_at
columns against the current date and time minus 1 year using the subYear
method.
You can use these scopes like so:
$posts = Post::createdAtLastYear()->get();
$updatedPosts = Post::updatedAtLastYear()->get();
See you in the next article
AK Newsletter
Join the newsletter to receive the latest updates in your inbox.