spatie/laravel-dashboard-belgian-trains-tile
Laravel Dashboard tile that shows Belgian train connections and their current status. Add it to your Spatie dashboard to monitor departures/arrivals and stay on top of delays and cancellations at a glance.
Installation
composer require spatie/laravel-dashboard-belgian-trains-tile
Publish the config (if needed):
php artisan vendor:publish --provider="Spatie\DashboardBelgianTrainsTile\DashboardBelgianTrainsTileServiceProvider"
Register the Tile
Add the tile to your dashboard in app/Providers/DashboardServiceProvider.php:
public function boot()
{
Dashboard::create()
->row()
->tile(\Spatie\DashboardBelgianTrainsTile\Tiles\BelgianTrainsTile::class)
->width(4) // Adjust width as needed
->endRow();
}
First Use Case
Visit your dashboard (/dashboard). The tile will display real-time Belgian train connections (e.g., departure/arrival times for a predefined station). Defaults to Brussels Central unless configured otherwise.
Customizing Station Data
Override the default station via config (config/dashboard-belgian-trains-tile.php):
'station' => 'ANTW', // Antwerp Central
Or dynamically in the tile class:
public function getStationCode(): string
{
return request('station', config('dashboard-belgian-trains-tile.station'));
}
Integration with API Responses The tile fetches data from the NMBS API. Cache responses for 5 minutes (default) to avoid rate limits:
// Override cache duration in config
'cache_minutes' => 10,
Styling and Layout
Extend the tile’s Blade template (resources/views/vendor/dashboard-belgian-trains-tile/tile.blade.php) to:
Conditional Rendering Disable the tile based on user roles or time (e.g., weekends):
public function shouldRender(): bool
{
return auth()->user()->can('view-trains') && now()->dayOfWeek !== \Carbon\Carbon::SUNDAY;
}
Event-Driven Updates Use Laravel events to refresh the tile dynamically (e.g., after a user action):
event(new \Spatie\DashboardBelgianTrainsTile\Events\RefreshTrainsTile);
API Rate Limits
429 errors.cache_minutes or implement a fallback (e.g., static data) in handle():
try {
return $this->fetchTrains();
} catch (\GuzzleHttp\Exception\RequestException $e) {
return view('dashboard-belgian-trains-tile::fallback', ['error' => 'API unavailable']);
}
Station Code Validation
BRUX) return empty data.getStationCode():
$validStations = ['BRUX', 'ANTW', 'GNT']; // Add your list
if (!in_array($station, $validStations)) {
throw new \InvalidArgumentException("Invalid station code.");
}
Time Zone Mismatches
{{ $train->departure->setTimezone(auth()->user()->timezone)->format('H:i') }}
Dashboard Compatibility
composer.json).composer.json:
"spatie/laravel-dashboard": "^2.0"
Local Testing Mock the API response for development:
// In a test or service provider
\Spatie\DashboardBelgianTrainsTile\BelgianTrainsTile::shouldReceive('fetchTrains')
->andReturn([/* Mock data */]);
Accessibility
<div role="region" aria-label="Belgian train connections for {{ $station }}">
Internationalization
// config/app.php
'dashboard-belgian-trains-tile' => [
'lang' => 'nl',
];
Performance
public function shouldLazyLoad(): bool
{
return true;
}
Extending Functionality
<input type="text" wire:model="station" placeholder="Enter station code">
Update the tile class to handle the live wire model:
public $station = 'BRUX';
Logging
catch (\Exception $e) {
\Log::error("Belgian Trains API failed: {$e->getMessage()}");
return view('...')->with('error', 'Service unavailable');
}
How can I help you explore Laravel packages today?