Fetch Weather Data in Laravel Projects
Published on by Paul Redmond
Laravel Weather by Henrik B Hansen allows you to fetch weather data from different weather providers in your Laravel projects. Here’s a quick example of what using this package looks like using the Facade:
// Weather response will always be a Collection of responses.// Forecast days depends on the weather service provider.$weather = weather()->getForecast($request); // Current weather$weather->first()->getCurrently();
You can customize the request when getting a forecast. Here’s an example from the readme:
use Vemcogroup\Weather\Providers\Provider;use Vemcogroup\Weather\Request as WeatherRequest;$request = new WeatherRequest('1 Infinite Loop, Cupertino, CA 95014, USA');$request->withUnits(Provider::WEATHER_UNITS_FAHRENHEIT);
Out of the box, Laravel Weather supports Dark Sky and weatherstack; however, it looks like the Dark Sky API is deprecated or is not providing new keys. From the blog it reads:
Our API service for existing customers is not changing today, but we will no longer accept new signups. The API will continue to function through the end of 2021
You can define your own provider to work with this package using the provided abstract class.
You can learn more about this package, get full installation instructions, and view the source code on GitHub at vemcogroup/laravel-weather.