Laravel 8 REST API with Passport Authentication Tutorial

In this tutorial you will learn about the Laravel 8 REST API with Passport Authentication Tutorial and its application with practical example.

In this Laravel 8 REST API with Passport Authentication Tutorial I will show you how to create REST API with passport authentication In laravel. In this tutorial you will learn to create rest api for crud operation with passport authentication In laravel 8 application. In this article I will share example to create rest api with passport authentication in laravel. I will also show you how to install passport auth package in laravel. After installing and configure passport authentication in laravel we will create simple crud operation rest api. In this article we will be creating a post management crud operation rest api with passport authentication in laravel.

Laravel 8 REST API with Passport Authentication Tutorial

In this step by step tutorial I will guide you through create a fully functional restful API with passport authentication in Laravel 8. We will be creating fully functional REST API along with passport Authentication. Please follow the instruction given below:

  1. Install New Laravel Project
  2. Set Up Database
  3. Install Passport Package
  4. Configure Passport Module
  5. Create Post Model & Run Migration
  6. Create a New Controller
  7. Define API Routes
  8. Test Laravel Passport API

Install New Laravel Project

First of all we need to create a fresh laravel project, download and install Laravel 8 using the below command

Set Up Database

Now, lets create a MySQL database and connect it with laravel application. After creating database we need to set database credential in application’s .env file.

Install Passport Package

In this step, we need to install Laravel Passport package via the composer dependency manager. Use the following command to install passport package.

Now, run following command to migrate database schema.

Now, it is mandatory to install passport using the command below. This command will generate encryption keys required to generate secret access tokens.

Configure Passport Module

Open App/User.php model file and add ‘Laravel\Passport\HasApiTokens’ trait in it.

Next, open app/Providers/AuthServiceProvider.php file and register the registerPolicies() method inside the boot() function, It will evoke the required routes.

Register the PassportServiceProvider class in providers array inside the config/app.php file:

Configure driver for the Passport, get inside the config/auth.php file and make the changes as shown below.

Create Posts Model & Run Migration

Now, in this step we will create model and migration file. Please run the following command:

Once above command is executed there will be a migration file created inside database/migrations/ directory, just open timestamp_create_posts_table.php migration file and update the function up() method as following:

Next, go to app/Models/Post.php file and add the $fillable property in the Post model as following.

Now, run the migration to create database table using following artisan command:

Create a New Controller

Now, lets create a controller named PassportAuthController using command given below –

Once the above command executed, it will create a controller file PassportAuthController.php in app/Http/Controllers/ directory. Open the PassportAuthController.php file and put the following code in it.

app/Http/Controllers/PassportAuthController.php

Now open app/Models/User.php file and put the following code in it:

Now, lets create a controller named PostController using command given below –


Add the following code in PostController.php file.

Define API Routes

Now we will create rest API auth and crud operation routes. Go to the routes directory and open api.php. Then put the following routes into api.php file:

routes/api.php

Test Laravel 8|7 Passport API

Now we are ready to run our example so lets start the development server using following artisan command –

Now, we will call above create crud and auth apis in postman app:

Register API:

Login Passport API:

Passport Post Create API:


You need to set this access token as a Bearer Token in the Authorization header.

Post List API:

Post Update API:

Post Delete API:

In this tutorial we have learn about the Laravel 8 REST API with Passport Authentication Tutorial and its application with practical example. I hope you will like this tutorial.