laracraft-tech/laravel-date-scopes
Add a DateScopes trait to Eloquent models to query records by common date ranges: today, last week, month-to-date, last year (with custom start), and more. Chain scopes with aggregates like sum/avg for fast stats-friendly queries.
DateScopes trait is lightweight and non-intrusive, allowing selective adoption per model (e.g., Transaction, Order, LogEntry).Transaction::ofLastWeek()->where('status', 'completed')) and aggregations (e.g., Transaction::ofToday()->sum('amount')).approved_at instead of created_at) and custom start dates, aligning with domain-specific needs.use DateScopes statement per model. No database migrations or schema changes required.custom_created_at).WHERE created_at BETWEEN ...). For large datasets, ensure indexes exist on timestamp columns (e.g., created_at).ofLastWeek).Carbon uses the app’s timezone by default. Ensure consistency across environments (e.g., config/app.php).ofLastCentury use Wikipedia’s convention (e.g., 20th century = 1901–2000). Validate alignment with business requirements.ofLastYear() scale for tables with millions of records? Test with EXPLAIN ANALYZE.ofLastBusinessWeek)? If so, evaluate maintainability of forks vs. contributing upstream.updated_at for "last modified") requiring custom scopes?UPGRADING.md for breaking changes.refreshDatabase() for time-sensitive tests).Model::where('created_at', '>=', now()->subDays(7)) with Model::ofLast7Days().LogEntry, AuditTrail) to validate performance and behavior.use DateScopes; to models and refactor queries.php artisan vendor:publish --tag="date-scopes-config") to set global defaults (e.g., inclusive/exclusive ranges).README or ADR.spatie/laravel-activitylog).ofLastWeek()). Prefix if needed (e.g., DateScopes::ofLastWeek()).created_at) are indexed for performance.config/app.php (e.g., timezone = 'UTC').composer.json and run composer install.DATE_SCOPES_DEFAULT_RANGE=inclusive).ofLastMonth()) using Laravel’s TestCase.ofLastBusinessWeek), document custom logic in a CUSTOM_SCOPES.md.php artisan test:scopes).config(['app.timezone', 'UTC']).config(['debugbar.enabled', true])).ofLastQuarter).customRange: DateRange::INCLUSIVE).ofLastYear() on tables >1M rows").metrics table).ofLastWeek()) using Laravel’s cache driver:
$
How can I help you explore Laravel packages today?