How to Create REST API in Laravel.

API is also known as Web services. Web services is very important when you are creating web and Mobile app. You require API in mobile application development. If you Don’t know how to create REST API in laravel then just follow the below Steps, i’m explaining everything step-by-step.  

Step 1: Install Laravel

In the first step, we require to get fresh Laravel application using below command, So open your terminal OR command prompt and run below command:

composer create-project --prefer-dist laravel/laravel laravel_api "5.4.*"

Step 2: Make Database Configuration

Step 3: Make Authentication scaffold

Make Laravel Authentication scaffold by below command and migrate

1. php artisan make:auth

2. php artisan make:auth

Step 4: Add Dummy Data in Table.

Add some dummy data in users table so that when we call our API then it return the dummy data.

Step 5: Create the Controller.

Now, lets make a Controller in which we call country table to call the data from table. first, create a directory named API and under api create a Controller and put the below code.


namespace App\Http\Controllers\api;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;
use App\User;

class apicontroller extends Controller
   public function index()
        return User::all();

See the below image of Controller.

Step 6: Create API Route.

Make api route to call our API. Go to routes/api.php and put the below code.

Route::get('countries', 'api\apicontroller@index');

See the below Image

Step 7: Call API Route.

Now we call the API route to access our Data. API URl is like

I’m running my project in local so my url is http://localhost/laravel_api/public/api/countries

Open the API url to access the data. See the Below Image