Weave Code
Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Weather Widget Laravel Package

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.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation

    composer require arshaviras/weather-widget
    

    Publish the package config (if needed):

    php artisan vendor:publish --provider="Arshaviras\WeatherWidget\WeatherWidgetServiceProvider" --tag="config"
    
  2. Basic Usage Register the widget in your Filament admin panel:

    use Arshaviras\WeatherWidget\Widgets\WeatherWidget;
    
    protected function getWidgets(): array
    {
        return [
            WeatherWidget::class,
        ];
    }
    
  3. First Use Case

    • Add the widget to a dashboard or resource page.
    • Configure the default location (e.g., config/weather-widget.php):
      'default_location' => 'New York, NY',
      'api_key' => env('WEATHER_API_KEY'),
      

Implementation Patterns

Common Workflows

  1. Dynamic Location Binding Fetch user-specific weather by binding to a user model:

    WeatherWidget::make()
        ->location(fn () => auth()->user()->location)
        ->units('metric'),
    
  2. Conditional Rendering Show/hide based on user roles or permissions:

    WeatherWidget::make()
        ->visible(fn () => auth()->user()->can('view_weather')),
    
  3. Custom Styling Override default CSS via Filament’s modify method:

    WeatherWidget::make()
        ->modify(function (WeatherWidget $widget) {
            $widget->iconSize = '2x';
            $widget->backgroundColor = 'bg-blue-50';
        }),
    
  4. API Key Management Use Laravel’s env() or a dedicated config file for API keys:

    'api_key' => env('OPENWEATHER_API_KEY'),
    

Integration Tips

  • Filament Panels: Works seamlessly with Filament’s dashboard and resource pages.
  • Livewire Components: Embed the widget in custom Livewire components by extending WeatherWidget.
  • Caching: Cache API responses for 15 minutes (adjustable in config):
    'cache_ttl' => 15, // minutes
    

Gotchas and Tips

Pitfalls

  1. API Key Exposure

    • Never hardcode API keys in Blade or JavaScript. Use Laravel’s env() or Filament’s getEnv().
    • Fix: Store keys in .env and restrict access via filament.php:
      'environment' => env('APP_ENV') !== 'local',
      
  2. Rate Limiting

    • Free-tier APIs (e.g., OpenWeatherMap) have strict rate limits (e.g., 60 calls/minute).
    • Fix: Cache responses aggressively or use a queue for high-traffic apps:
      'use_queue' => env('APP_ENV') === 'production',
      
  3. Location Parsing Errors

    • Invalid city names (e.g., typos) return null. Validate inputs:
      $widget->location('New York, NY')->validateLocation();
      
  4. CSS Conflicts

    • Tailwind classes may clash with Filament’s default styles.
    • Fix: Use !important sparingly or scope styles:
      .weather-widget { @apply relative; }
      

Debugging

  • API Failures: Check storage/logs/laravel.log for HTTP errors (e.g., 401 Unauthorized).
  • Missing Data: Verify config/weather-widget.php has a valid api_key and default_location.
  • Widget Not Rendering: Ensure the widget is registered in getWidgets() and the panel is refreshed.

Extension Points

  1. 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
        }
    }
    
  2. Additional Weather Metrics Add custom fields to the widget’s query:

    WeatherWidget::make()
        ->extraFields(['wind_speed', 'humidity']),
    
  3. 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';
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
adachsoft/changelog-linter
adachsoft/collection
payerurl/telegrambot-crypto-payment-instant-payout
dskripchenko/laravel-hybrid-search
riorizkyrainey/lara-ced
mehedimi/static-vel
zharp/zharp
nonsapiens/bigquery-model-sync
lgu-bayambang-ict-office/lgu-laravel-development-standards
andydefer/laravel-support
se7enxweb/flysystem
lampwick/laravel
jeslage/statamic-emojis
micahdshackelford/rmap-laravel
draftsmaninc/draftsman
square-bit/laravel-volition
himito/laravel-mailman
htinlynn/genius-helper
maco-studios/hephaestus
adachsoft/php-code-style