spatie/laravel-dashboard-velo-tile
Laravel Dashboard tile for showing the current status of Velo, Antwerp’s bike sharing system. Plug it into your dashboard to surface Velo availability/health at a glance.
Installation:
composer require spatie/laravel-dashboard-velo-tile
Ensure spatie/laravel-dashboard is installed (this package is a tile for it).
Register the Tile:
Add the tile to your dashboard configuration in config/dashboard.php:
'tiles' => [
\Spatie\Dashboard\Tiles\VeloTile::class,
// ... other tiles
],
First Use Case: Display real-time Velo bike availability on your dashboard. The tile fetches and renders data automatically—no manual API calls needed.
config/dashboard.php for tile-specific settings (e.g., API endpoints, caching).\Spatie\Dashboard\Tiles\VeloTile for customization hooks (e.g., modifying the API request or response parsing).Data Fetching:
The tile uses Spatie’s Http facade to call the Velo API. Override the getData() method in a custom tile class to modify the request:
use Spatie\Dashboard\Tiles\VeloTile;
class CustomVeloTile extends VeloTile {
protected function getData(): array {
$response = $this->http->get('https://api.velo.be/v2/stations', [
'query' => ['station_id' => 123],
]);
return $response->json();
}
}
Rendering:
The tile renders data in a predefined Blade template (resources/views/vendor/dashboard/tiles/velo.blade.php). Extend this template by publishing and overriding it:
php artisan vendor:publish --tag=dashboard-tiles
Customize the view in resources/views/vendor/dashboard/tiles/velo.blade.php.
Integration with Dashboard:
shouldBeDisplayed() to show/hide the tile based on user roles or settings:
protected function shouldBeDisplayed(): bool {
return auth()->user()->can('view-velo-dashboard');
}
Caching:
Leverage Laravel’s cache to reduce API calls. Configure in config/dashboard.php:
'tiles' => [
\Spatie\Dashboard\Tiles\VeloTile::class => [
'cache_for' => '10 minutes', // Cache data for 10 minutes
],
],
Multi-Station Tracking:
Fetch data for multiple stations and merge it in getData():
protected function getData(): array {
$stations = [123, 456, 789];
$data = collect($stations)->map(fn ($id) => $this->http->get("https://api.velo.be/v2/stations/{$id}"));
return $data->pluck('json')->all();
}
Custom Styling: Use CSS variables or inline styles in the published Blade template to match your dashboard’s theme.
Event-Driven Updates: Listen for Velo API updates via webhooks (requires custom backend logic) and trigger tile refreshes via JavaScript:
Echo.channel('velo-updates')
.listen('VeloUpdated', (data) => {
window.location.reload(); // Force tile refresh
});
API Rate Limiting:
cache_for) aggressively or implement exponential backoff in getData().429 Too Many Requests errors and handle them gracefully.Data Parsing Issues:
getData():
$response = $this->http->get('...');
$data = $response->json();
if (!isset($data['stations'])) {
throw new \RuntimeException('Invalid API response format');
}
Time Zone Mismatches:
{{ $station['last_update']->setTimezone(auth()->user()->timezone) }}
Dashboard Compatibility:
composer.json for constraints.Log API Responses:
Add debug logs in getData() to inspect raw API responses:
\Log::debug('Velo API Response:', $response->json());
Test Locally: Use Laravel’s HTTP client to mock the Velo API during development:
$this->http->fake([
'https://api.velo.be/v2/stations' => Http::response([
'stations' => [['id' => 123, 'free_bikes' => 5]],
]),
]);
Check Network Requests: Use browser dev tools (Network tab) to verify the tile’s API calls and payloads.
Custom Tile Class:
Extend \Spatie\Dashboard\Tiles\VeloTile to add features like:
Blade Template Hooks: Override the published Blade template to:
Configuration:
Expose tile settings via config/dashboard.php:
'tiles' => [
\Spatie\Dashboard\Tiles\VeloTile::class => [
'default_station_id' => 123,
'show_map' => true,
],
],
Access these in your custom tile class via config('dashboard.tiles.velo').
Localization: Translate tile labels and messages using Laravel’s localization features:
{{ __('dashboard::velo.free_bikes', ['count' => $station['free_bikes']]) }}
Performance: Lazy-load the tile’s JavaScript/CSS to improve dashboard load times. Use Laravel Mix to split assets.
Accessibility: Ensure the tile’s Blade template adheres to WCAG standards (e.g., ARIA labels for interactive elements).
Documentation: Add inline PHPDoc comments to your custom tile class for future maintainability:
/**
* Fetches Velo station data with custom query parameters.
*
* @return array Raw API response data.
* @throws \RuntimeException If API response is invalid.
*/
protected function getData(): array { ... }
How can I help you explore Laravel packages today?