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) ensures minimal boilerplate and avoids polluting the global namespace. This is ideal for modular Laravel applications where traits are preferred over base classes for inheritance.approved_at), inclusive/exclusive ranges, and fluent configuration, which aligns with Laravel’s flexibility for domain-specific adaptations.created_at column). It can be adopted incrementally, model-by-model.where clauses remain unaffected.ofLastWeek() may misalign with business expectations (e.g., "last 7 days" might be interpreted as inclusive in analytics). Requires stakeholder validation.where clauses, which could impact query performance on large datasets if not indexed. Requires proper database indexing (e.g., created_at).now() uses the app’s timezone (configurable in Laravel). May need validation for multi-timezone apps.approved_at) requires explicit configuration per model, adding maintenance overhead.created_at columns indexed? If not, will this package introduce performance bottlenecks?ofLastWeek()->sum('amount')), requiring optimized database stats?published_at) be managed across the codebase?where('created_at', '>=', ...)) to identify candidates for replacement.Transaction) to test the trait and scopes.php artisan vendor:publish --tag="date-scopes-config") to set global defaults (e.g., default_range).Transaction::ofLastWeek(customRange: DateRange::INCLUSIVE)).where clauses with scopes in new code.deprecate() helper to warn about legacy queries in CI/CD.created_at).composer require laracraft-tech/laravel-date-scopes.use LaracraftTech\LaravelDateScopes\DateScopes.default_range in .env).Transaction::ofLastWeek()).composer update cautiously.README or wiki.->toSql() on queries to verify generated SQL.created_at (or custom columns) are indexed. Add composite indexes if queries filter by multiple date ranges.Transaction::ofLastWeek()) using Laravel’s cache or Redis.config(['app.timezone' => 'UTC']) consistently.approved_at vs. approvedOn) will cause silent failures. Validate in tests.ofLastWeek(), monthToDate()) and configuration.where clauses).public function test_of_last_week()
{
$oldTransaction = Transaction::factory()->create();
$newTransaction = Transaction::factory()->create();
$this->assertCount(1, Transaction::ofLastWeek()->get());
// Ensure $newTransaction is excluded if using exclusive range
}
ofLastMonth()->where('status', 'active')).ofToday() returns no results, check your timezone.").How can I help you explore Laravel packages today?