spatie/laravel-dashboard-time-weather-tile
Time & Weather Tile for Spatie Laravel Dashboard. Shows the current time and local weather on your dashboard, with simple setup and configuration. Ideal for wall-mounted displays and status screens.
Install Dependencies:
composer require spatie/laravel-dashboard-time-weather-tile
Ensure spatie/laravel-dashboard (v3.x+) is installed.
Publish Configuration (if customizing):
php artisan vendor:publish --provider="Spatie\DashboardTimeWeatherTile\DashboardTimeWeatherTileServiceProvider"
Edit .env for API keys (e.g., OPENWEATHERMAP_API_KEY).
Register the Tile:
Add to your dashboard configuration (e.g., app/Providers/DashboardServiceProvider):
use Spatie\Dashboard\Dashboard;
use Spatie\DashboardTimeWeatherTile\DashboardTimeWeatherTile;
public function boot()
{
Dashboard::create()
->withTile(DashboardTimeWeatherTile::new());
}
First Use Case: Display a tile showing local time (timezone-aware) and current weather (e.g., temperature, emoji) on your dashboard. Test with:
php artisan dashboard
DashboardTimeWeatherTile (customization entry point).resources/views/vendor/dashboard-time-weather-tile.blade.php (override for UI tweaks)..env (API keys) and config/dashboard-time-weather-tile.php (units, caching).Basic Integration:
config/app.timezone is set correctly (e.g., America/New_York) for accurate time display.API Configuration:
// app/Providers/DashboardServiceProvider.php
use Spatie\DashboardTimeWeatherTile\DashboardTimeWeatherTile;
Dashboard::create()
->withTile(DashboardTimeWeatherTile::new()
->useWeatherProvider(new CustomWeatherProvider())
);
.env:
OPENWEATHERMAP_API_KEY=your_key_here
Localization:
App::getLocale() for date formatting (no additional config needed post-v1.0.1).Caching:
DashboardTimeWeatherTile::new()
->cacheFor(seconds: 300) // Custom TTL
Timezone Handling:
config/app.timezone. Override per-user with:
DashboardTimeWeatherTile::new()
->timezone('Europe/Paris') // Static override
Dashboard Layout: Place the tile in a dedicated column or sidebar for minimal disruption:
Dashboard::create()
->column()
->withTile(DashboardTimeWeatherTile::new())
->endColumn()
Conditional Rendering: Show/hide based on user roles or regions:
if (auth()->user()->isAdmin()) {
Dashboard::create()->withTile(DashboardTimeWeatherTile::new());
}
Testing: Mock API responses in PHPUnit:
$this->mockWeatherApi()
->shouldReturn(['temp' => 20, 'weather' => ['icon' => '01d']]);
Extending Functionality: Add indoor temperature (v1.3.0+):
DashboardTimeWeatherTile::new()
->withInsideTemperature(22) // Celsius
Timezone Mismatches:
config/app.timezone is misconfigured.APP_TIMEZONE in .env and test with:
php artisan tinker
>>> \Carbon\Carbon::now()->timezone(config('app.timezone'));
API Failures:
try-catch:
try {
$weather = $provider->getWeather();
} catch (\Exception $e) {
$weather = null; // Fallback to cached data or placeholder
}
Rate Limits:
cacheFor(300)) and monitor usage via API dashboard.Blade Rendering Quirks:
{!! html_entity_decode('🌡') !!} <!-- Sun emoji -->
Inside Temperature:
insideTemperature span appears even when unset (pre-v2.0.2).@if($insideTemperature)
<span>{{ $insideTemperature }}°C</span>
@endif
API Debugging:
Log raw API responses to .env:
\Log::debug('Weather API Response:', $weatherData);
Check logs with:
tail -f storage/logs/laravel.log
Timezone Debugging: Dump the active timezone:
\Log::info('Active Timezone:', config('app.timezone'));
Caching Issues: Clear cached views and config:
php artisan view:clear
php artisan config:clear
Custom Weather Providers:
Implement Spatie\DashboardTimeWeatherTile\Contracts\WeatherProvider:
use Spatie\DashboardTimeWeatherTile\Contracts\WeatherProvider;
class CustomWeatherProvider implements WeatherProvider {
public function getWeather(): array {
return ['temp' => 18, 'weather' => ['icon' => '02n']];
}
}
Blade Overrides:
Copy the default template to resources/views/vendor/dashboard-time-weather-tile.blade.php and modify:
<div class="tile">
<!-- Customize here -->
<div class="time">{{ now()->format('H:i') }}</div>
<div class="weather">🌤️ {{ $temperature }}°C</div>
</div>
Dynamic Configuration: Fetch API keys or settings from the database:
$apiKey = \App\Models\Setting::first()->weather_api_key;
config(['dashboard-time-weather-tile.api_key' => $apiKey]);
Unit Testing: Mock the weather provider in tests:
$this->app->bind(
\Spatie\DashboardTimeWeatherTile\Contracts\WeatherProvider::class,
function () {
return new class implements WeatherProvider {
public function getWeather(): array {
return ['temp' => 15, 'weather' => ['icon' => '03d']];
}
};
}
);
Units:
Defaults to Celsius. Change in config/dashboard-time-weather-tile.php:
'units' => 'fahrenheit',
Environment Variables:
Prefix API keys with DASHBOARD_TIME_WEATHER_TILE_ to avoid conflicts:
DASHBOARD_TIME_WEATHER_TILE_OPENWEATHERMAP_API_KEY=your_key
Dashboard Version:
Ensure compatibility with spatie/laravel-dashboard:
spatie/laravel-dashboard-time-weather-tile v4.0.0+.How can I help you explore Laravel packages today?