laracraft-tech/laravel-date-scopes
Add powerful, ready-made date query scopes to Laravel Eloquent models. Use the DateScopes trait to query records for today, last week, month-to-date, last year (with custom start dates), and more—fully chainable with builder methods and aggregations.
Pros:
created_at alternatives), and per-query overrides. Flexible for domain-specific requirements (e.g., financial year vs. calendar year).Transaction::ofLastWeek()->where(...)->sum('amount')), fitting Laravel’s fluent query style.sum, avg, count) on scoped datasets, critical for dashboards or batch processing.Cons:
DB::select). Requires wrapper methods if used outside Eloquent.config/app.php). Edge cases (e.g., multi-region apps) may need manual adjustments.Low Risk:
composer require with zero breaking changes (MIT license, no runtime hooks).created_at or custom timestamp columns. No migrations required.composer test or mock queries.Potential Challenges:
published_at).WHERE clauses. For large tables, ensure indexes exist on scoped columns (e.g., created_at).| Risk Area | Severity | Mitigation |
|---|---|---|
| Inclusive/Exclusive Logic | Medium | Validate with stakeholders; use customRange parameter for overrides. |
| Time Zone Handling | Low | Ensure config/app.php timezone matches business requirements. |
| Query Performance | Medium | Test with EXPLAIN on large datasets; add indexes to scoped columns. |
| Custom Column Support | Low | Document per-model configurations (e.g., use DateScopes + const CREATED_AT). |
| Laravel Version Lock | Low | Pin version in composer.json (e.g., ^2.5). |
event_date instead of created_at)?where('created_at', '>', ...)). Identify candidates for replacement.LogEntry, Notification).LogEntry::ofLastWeek() vs. whereBetween).use DateScopes; to target models.Model::ofLastMonth()).where clauses with scope calls).php artisan vendor:publish --tag="date-scopes-config") if default range (exclusive) doesn’t match needs.DATE_SCOPES_DEFAULT_RANGE=inclusive in .env if required.^2.5 in composer.json for stability.ofLastWeek() methods in models).where clauses (e.g., where('created_at', '>=', now()->subDays(7)) → ofLast7Days()).where(...)->sum('amount') → ofLastMonth()->sum('amount')).published_at) via column parameter.ofLastQuarter() are more readable than raw SQL.default_range) may need updates if business rules change.dd($query->toSql())).config/app.timezone).->toSql() to inspect generated queries:
Transaction::ofLastWeek()->toSql(); // Verify WHERE clauses.
ofLastYear() for annual reports").created_at (or custom column) is indexed. Example:
Schema::table('transactions', function (Blueprint $table) {
$table->index('created_at');
});
EXPLAIN ANALYZE:
DB::enableQueryLog();
Transaction::ofLastYear
How can I help you explore Laravel packages today?