alexmg86/laravel-sub-query
alexmg86/laravel-sub-query brings fluent subquery support to Laravel's query builder and Eloquent, letting you embed subqueries in SELECT, FROM, WHERE, JOIN, and ORDER BY clauses through a clean, chainable API.
v1.9.9 normally. This release is additive: it adds whereCurrentYear, whereCurrentMonth, and whereCurrentDay; no breaking changes are indicated.whereCurrentYear('created_at'), and whether they are query-builder macros or Eloquent scopes.Order::whereCurrentYear('placed_at')->count();
Order::whereCurrentMonth('placed_at')->count();
Order::whereCurrentDay('placed_at')->count();
Use the new methods as convenience date scopes on Eloquent queries, always passing the business-relevant timestamp column explicitly:
$revenueThisMonth = Order::query()
->whereCurrentMonth('paid_at')
->where('status', 'paid')
->sum('total');
Build dashboard cards or report filters around them:
$query = Post::query()->where('is_published', true);
if ($range === 'today') {
$query->whereCurrentDay('published_at');
} elseif ($range === 'month') {
$query->whereCurrentMonth('published_at');
} elseif ($range === 'year') {
$query->whereCurrentYear('published_at');
}
$posts = $query->latest('published_at')->paginate();
Encapsulate repeated business meaning in model scopes rather than scattering the raw methods:
public function scopePublishedThisYear($query)
{
return $query->whereCurrentYear('published_at');
}
Use them in scheduled commands and reports where “current” means the calendar year/month/day at execution time:
$dailySignups = User::whereCurrentDay('created_at')->count();
For tests, freeze time and verify boundaries:
Carbon::setTestNow('2025-03-01 00:00:00');
$this->assertSame(0, Order::whereCurrentMonth('placed_at')->count());
YEAR(column) = ?, MONTH(column) = ?, or DAY(column) = ?, they may prevent normal index usage on large tables. Prefer explicit whereBetween ranges for high-volume queries when performance matters.whereCurrentDay means calendar day, not “last 24 hours.” If you need rolling windows, use explicit now()->subDay() boundaries instead.created_at is rarely the right business column for reporting; use paid_at, published_at, shipped_at, etc.null values; verify this matches report expectations.How can I help you explore Laravel packages today?