To display the current date and time on a Filament dashboard, you can create a custom widget. Filament allows you to create custom widgets by extending the Widget class. Here's a simple example of how you can create a widget to show the current date and time:
- Create a new widget class. You can place this file in the
app/Filament/Widgetsdirectory or any other directory where you organize your Filament widgets.
namespace App\Filament\Widgets;
use Filament\Widgets\Widget;
use Carbon\Carbon;
class CurrentDateTimeWidget extends Widget
{
protected static string $view = 'filament.widgets.current-date-time-widget';
public function getCurrentDateTime()
{
return Carbon::now()->toDayDateTimeString();
}
}
- Create a Blade view for the widget. Create a new Blade file named
current-date-time-widget.blade.phpin theresources/views/filament/widgetsdirectory.
<div class="card">
<div class="card-body">
<h5 class="card-title">Current Date & Time</h5>
<p class="card-text">{{ $this->getCurrentDateTime() }}</p>
</div>
</div>
- Register the widget in your Filament dashboard. Open the
FilamentServiceProvider(or create one if it doesn't exist) and register the widget in thebootmethod.
namespace App\Providers;
use App\Filament\Widgets\CurrentDateTimeWidget;
use Filament\FilamentServiceProvider as BaseFilamentServiceProvider;
use Filament\Widgets\Widget;
class FilamentServiceProvider extends BaseFilamentServiceProvider
{
protected function getDashboardWidgets(): array
{
return [
// ... other widgets
Widget::make(CurrentDateTimeWidget::class),
];
}
}
- Make sure to import the
Carbonfacade at the top of your widget class if you haven't already:
use Carbon\Carbon;
Now, when you visit your Filament dashboard, you should see the widget displaying the current date and time. Remember that the time shown will be based on the server's timezone settings unless you specify a different timezone using Carbon's methods.