aymanalhattami/filament-date-scopes-filter
Install Dependencies:
composer require aymanalhattami/filament-date-scopes-filter laracraft/laravel-date-scopes
laracraft/laravel-date-scopes, so ensure your models use date scopes (e.g., use Laracraft\DateScopes\HasDateScopes;).Publish Translations (Optional):
php artisan vendor:publish --tag="filament-date-scopes-filter-translations"
First Use Case: Add the filter to a Filament table resource:
use AymanAlhattami\FilamentDateScopesFilter\DateScopeFilter;
public static function table(Table $table): Table
{
return $table
->filters([
DateScopeFilter::make('created_at'), // Replace with your timestamp column
]);
}
Basic Filtering:
DateScopeFilter::make('published_at') // Filters by a custom column
->label('Publication Date'); // Custom label
Excluding Scopes:
DateScopeFilter::make('updated_at')
->withoutSeconds()
->withoutMinutes()
->withoutHours();
Multi-Column Filtering:
$table->filters([
DateScopeFilter::make('start_date'),
DateScopeFilter::make('end_date'),
]);
Dynamic Scopes:
Use laravel-date-scopes to define custom scopes in your model:
// In your model
use HasDateScopes;
protected $dateScopes = [
'custom_scope' => 'whereDate(\'created_at\', \'=\', $date)',
];
Then reference them in the filter:
DateScopeFilter::make('created_at')->scopes(['custom_scope']);
Integration with Actions: Chain filters with actions (e.g., export filtered data):
$table->actions([
Tables\Actions\ExportAction::make(),
]);
Conditional Scopes: Dynamically adjust available scopes based on user roles or context:
DateScopeFilter::make('created_at')
->scopes(fn () => auth()->user()->isAdmin() ? ['all'] : ['daily']);
Custom UI: Override the default dropdown with a custom component:
DateScopeFilter::make('created_at')
->customComponent(MyCustomDateScopeComponent::class);
Localization: Extend translations for non-English locales:
// config/filament-date-scopes-filter.php
'translations' => [
'es' => [
'seconds' => 'Segundos',
// ...
],
];
Missing laravel-date-scopes:
Class 'HasDateScopes' not found.laracraft/laravel-date-scopes and ensure models use HasDateScopes.Column Mismatch:
created_at must be DATE/DATETIME).Scope Overlap:
hours and days) may cause unexpected queries.without* methods to exclude redundant scopes or test queries with dd($query).Translation Conflicts:
php artisan vendor:publish and clear cached views (php artisan view:clear).Filament Version Mismatch:
Query Inspection: Use Filament’s query logging to debug scope application:
// In your model
protected static function booted()
{
static::addGlobalScope(new \Illuminate\Database\Eloquent\Scope {
public function apply($query, \Illuminate\Database\Eloquent\Model $model)
{
\Log::info('Query:', [$query->toSql(), $query->getBindings()]);
return $query;
}
});
}
Scope Validation:
Validate scopes in your model’s booted() method:
public static function boot()
{
static::addGlobalScope(new \Laracraft\DateScopes\Scopes\DateScope);
// Ensure scopes are registered before filtering
}
Performance:
withoutSeconds()) to reduce query complexity.created_at).Custom Scopes:
Extend laravel-date-scopes to add domain-specific scopes (e.g., "this quarter"):
// In your model
public function scopeThisQuarter($query)
{
return $query->whereYear('created_at', date('Y'))
->whereMonth('created_at', date('n'));
}
Then use it in the filter:
DateScopeFilter::make('created_at')->scopes(['thisQuarter']);
UI Customization: Override the filter’s Blade template:
DateScopeFilter::make('created_at')
->view('custom.date-scope-filter');
resources/views/filament-date-scopes-filter/date-scope-filter.blade.php).Dynamic Column Mapping: Map database columns to user-friendly labels:
DateScopeFilter::make('created_at')
->label('Record Creation Date');
Event Listeners: Listen for filter changes to trigger side effects (e.g., update a dashboard):
use AymanAlhattami\FilamentDateScopesFilter\Events\DateScopeFiltered;
DateScopeFiltered::listen(function ($filter) {
// Update analytics, etc.
});
How can I help you explore Laravel packages today?