How to Fetch Data from an API in Laravel

How to Fetch Data from an API in Laravel

With Laravel it's quite easy to fetch data from an external API.

In order to fetch data from an API and display it to the user it's quite easy just follow the below steps:

  • Installing a Fresh Laravel Application
  • Creating a Route
  • Creating a Controller
  • Fetching data from an API and Passing it to the View
  • Creating a View
  • Conclusion

So let's get start

Step 1: Create a fresh laravel application or you can use your existing project, there are several ways to install a Laravel application I will use the composer, run the following command in the command line:

composer create-project laravel/laravel example-app

Congrats! now you have a fresh laravel application. now let's move on to the second step.

Step 2: Now let's create a route

Route::get('/', [PageController::class,'index'])->name('index');

In the above snippet we have created a route named index this route will load the root page of our application and for the action, it will call the index function inside a PageController, controller so let's create the controller

Step3: For creating PageController run the following command in the command line:

php artisan make:controller PageController

this command will create a controller named PageController inside the controllers' folder. Now so let's move to the next step

Step4: Laravel has evolved significantly since the release of version 7. x.x. One of the key areas of improvement revolves around the Guzzle HTTP Client.

Imagine you are building a web application, and you want it to communicate with other online services. For instance, you may want to fetch movies from the Movie Database, or in this example fetch dog breeds from the dog API and display them in your application. How do you achieve this in PHP?

Well, as you might have already guessed, Guzzle HTTP Client is the simple solution to the above problem. Guzzle HTTP client allows your application to make HTTP requests.

This step is the more important part of our article in this step we are going to fetch data from the API, I'm going to use This Dog Breeds API and we will page 5 random dog breeds and will display it in the view. Now inside PageController let's create a function called index and we will fetch the data from the above API

<?php

namespace App\Http\Controllers;

use Illuminate\Support\Facades\Http;

class PageController extends Controller
{

    public function index(){
        // fetching data from the api with 5 random breeds
        $breeds = Http::get('https://dog.ceo/api/breeds/list/random/5')['message'];
        return view('index',compact('breeds'));
    }

}

Congratulations you have successfully fetch the data from API and passed it to the view so let's create the view and display the fetched data.

Step5: Let's create a view named index.blade.php inside the view folder and loop through the $breeds variable

 <div class="row wide-xl " >
            @foreach($breeds as $breed)
                <div class="col-lg-2 ">
                    <div class="feature-with-icon" data-aos="flip-up" >
                        <h5><strong>{{ucfirst($breed)}}</strong></h5>
                    </div>
                </div>
            @endforeach
        </div>

Conclusion: So we easily fetched the data from the API and displayed in the view the final look of our article will look like this:

FireShot Capture 004 - Dog Breeds - Home - 127.0.0.1.png

You can clone the project source code from This Github Repo

Note: This is my first attempt at writing I know it may have problems my main goal behind this article was just trying to start from somewhere, hope to receive your constructive comments, feedback, and tips. Thank You :)