spatie/laravel-dashboard-time-weather-tile
Laravel Dashboard tile by Spatie that shows current time plus local weather. Plug it into your dashboard to keep an eye on conditions at a glance, with configuration handled via the Laravel Dashboard ecosystem.
Installation:
composer require spatie/laravel-dashboard-time-weather-tile
Ensure 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\TimeWeatherTile::class,
],
First Use Case:
php artisan vendor:publish --provider="Spatie\Dashboard\DashboardServiceProvider"
.env:
DASHBOARD_WEATHER_API_KEY=your_api_key_here
DASHBOARD_WEATHER_LOCATION=your_city
/dashboard) to see the tile displaying local time and weather.Dynamic Tile Integration:
laravel-dashboard tiles in a single dashboard view. Example:
// In a controller or blade view
use Spatie\Dashboard\Tiles\TimeWeatherTile;
$tiles = [
new TimeWeatherTile(),
new OtherTile(),
];
return view('dashboard', compact('tiles'));
Customizing Appearance:
php artisan vendor:publish --tag=dashboard-views
resources/views/vendor/dashboard/tiles/time-weather.blade.php to adjust layout, colors, or icons.Conditional Rendering:
if (auth()->user()->canSeeWeather()) {
$tiles[] = new TimeWeatherTile();
}
Localization:
// In config/dashboard.php
'tiles' => [
(new TimeWeatherTile())->setTimeLabel(__('dashboard.time')),
],
API Key Management:
.env and use Laravel’s config() helper:
$tile = new TimeWeatherTile(config('dashboard.weather_api_key'));
Weather Service Flexibility:
Extend the tile to support multiple weather APIs (e.g., AccuWeather) by implementing a WeatherService contract and binding it in the DashboardServiceProvider.
Caching: Cache weather data to reduce API calls (e.g., using Laravel’s cache):
$weather = Cache::remember('weather_data', now()->addHours(1), function () {
return $this->weatherService->getWeather();
});
Testing: Mock the weather service in tests:
$this->mock(WeatherService::class, function ($mock) {
$mock->shouldReceive('getWeather')->andReturn(['temp' => 20, 'condition' => 'sunny']);
});
API Key Leaks:
.env and Laravel’s config system. Avoid logging or exposing keys in client-side code.Rate Limiting:
Time Zone Mismatches:
$tile = new TimeWeatherTile();
$tile->setTimeZone(auth()->user()->timezone);
Missing Dependencies:
Class 'Spatie\Dashboard\Tiles\TimeWeatherTile' not found.laravel-dashboard is not installed or autoloaded.composer dump-autoload and ensure laravel-dashboard is in composer.json.Weather Data Errors:
.env keys are correct.weather vs. main.temp).\Log::debug('Weather API response:', $this->weatherService->getWeather());
Log API Responses:
Add temporary logging in the tile’s render() method to inspect data:
\Log::debug('Weather data:', $weatherData);
Validate Config:
Ensure config/dashboard.php includes:
'weather' => [
'api_key' => env('DASHBOARD_WEATHER_API_KEY'),
'location' => env('DASHBOARD_WEATHER_LOCATION', 'New York'),
'unit' => env('DASHBOARD_WEATHER_UNIT', 'metric'), // or 'imperial'
],
Unit Testing: Test edge cases like:
Custom Weather Icons: Override the default icons by extending the tile:
class CustomTimeWeatherTile extends TimeWeatherTile {
public function getWeatherIcon($condition) {
return match ($condition) {
'rain' => '☔', // Custom icon
default => parent::getWeatherIcon($condition),
};
}
}
Additional Data: Extend the tile to show humidity, wind speed, or forecasts:
// In the tile's render method
$forecast = $this->weatherService->getForecast();
return view('dashboard::tiles.time-weather', [
'forecast' => $forecast,
]);
Dark Mode Support: Dynamically adjust tile styling based on user preferences:
$tile->setDarkMode(auth()->user()->prefers_dark_mode);
Geolocation: Auto-detect user location (with permission) and update the weather tile:
// Frontend JavaScript
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(position => {
axios.post('/update-weather-location', {
lat: position.coords.latitude,
lng: position.coords.longitude,
});
});
}
How can I help you explore Laravel packages today?