arshaviras/weather-widget
Laravel package that adds a simple weather widget to your app. Provides an easy way to display current weather details in a reusable component, suitable for dashboards or public pages, with straightforward setup and configuration.
Installation
composer require arshaviras/weather-widget
Publish the package config (if needed):
php artisan vendor:publish --provider="Arshaviras\WeatherWidget\WeatherWidgetServiceProvider" --tag="config"
Basic Usage Register the widget in your Filament admin panel:
use Arshaviras\WeatherWidget\Widgets\WeatherWidget;
protected function getWidgets(): array
{
return [
WeatherWidget::class,
];
}
First Use Case
config/weather-widget.php):
'default_location' => 'New York, NY',
'api_key' => env('WEATHER_API_KEY'),
Dynamic Location Binding Fetch user-specific weather by binding to a user model:
WeatherWidget::make()
->location(fn () => auth()->user()->location)
->units('metric'),
Conditional Rendering Show/hide based on user roles or permissions:
WeatherWidget::make()
->visible(fn () => auth()->user()->can('view_weather')),
Custom Styling
Override default CSS via Filament’s modify method:
WeatherWidget::make()
->modify(function (WeatherWidget $widget) {
$widget->iconSize = '2x';
$widget->backgroundColor = 'bg-blue-50';
}),
API Key Management
Use Laravel’s env() or a dedicated config file for API keys:
'api_key' => env('OPENWEATHER_API_KEY'),
WeatherWidget.'cache_ttl' => 15, // minutes
API Key Exposure
env() or Filament’s getEnv()..env and restrict access via filament.php:
'environment' => env('APP_ENV') !== 'local',
Rate Limiting
'use_queue' => env('APP_ENV') === 'production',
Location Parsing Errors
null. Validate inputs:
$widget->location('New York, NY')->validateLocation();
CSS Conflicts
!important sparingly or scope styles:
.weather-widget { @apply relative; }
storage/logs/laravel.log for HTTP errors (e.g., 401 Unauthorized).config/weather-widget.php has a valid api_key and default_location.getWidgets() and the panel is refreshed.Custom Data Sources
Extend the WeatherData class to support alternative APIs (e.g., WeatherAPI.com):
class CustomWeatherData extends WeatherData
{
protected function fetchData(string $location): array
{
// Custom logic here
}
}
Additional Weather Metrics Add custom fields to the widget’s query:
WeatherWidget::make()
->extraFields(['wind_speed', 'humidity']),
Geolocation Fallback Use the user’s IP to auto-detect location:
use Illuminate\Support\Facades\Http;
$ip = request()->ip();
$location = Http::get("http://ip-api.com/json/{$ip}")->json()['city'] ?? 'Unknown';
How can I help you explore Laravel packages today?