Laravel 8 Rest API CRUD with Passport Auth Tutorial

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

In this Laravel 8 Rest API CRUD with Passport Auth Tutorial tutorial, I will show you how to create rest api in laravel 8 application with passport authentication. In this tutorial we will be using passport for api authentication. we will create register and login api with simple retrieve user details.

Laravel 8 Rest CRUD APIs Tutorial With Example

  • Download Laravel 8 App
  • Database Configuration
  • Install Passport Auth
  • Passport Configuration
  • Create Product Table and Model
  • Run Migration
  • Create Auth and CRUD APIs Route
  • Create Passport Auth and CRUD Controller
  • Test Laravel 8 REST CRUD API with Passport Auth in Postman

Install Laravel 8

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

Configure Database In .env file

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.

After Installing ‘laravel/passport’ package, we need to add service provider in config/app.php file as following.

config/app.php

Run Migration and Install Laravel Passport

After successfully installing ‘laravel/passport’ package, we require to create default passport tables in our database. so let’s run the following command to migrate Laravel Passport tables to your database.

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

Laravel Passport Configuration

Now, we need to make following changes in our model, service provider and auth config file to complete passport configuration. Open App/User.php model file and add ‘Laravel\Passport\HasApiTokens’ trait in it.

Next Register passport routes in App/Providers/AuthServiceProvider.php, open App/Providers/AuthServiceProvider.php and put “Passport::routes()” inside the boot method like below.

Now open config/auth.php file and set api driver to passport instead of session.

Create Product Table and Model

Now we will create product model and migration file, open terminal and run the following command:

After that, navigate to database/migrations directory and open create_products_table.php file. Then put the following code in it:

Now, add fillable property in product.php file, so go to app/models directory and open product.php file and put the following code into it:

Run Migration

Now you need to run the migration using the following command. This command will create tables in the database :

Create Auth and CRUD APIs Route

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

Create Passport Auth and CRUD Controller

In this step, we will create a controllers name PassportAuthController and ProductController. Use the following command to create a controller :

After that, Create some authentication methods in PassportAuthController.php. So navigate to app/http/controllers/API directory and open PassportAuthController.php file. And, update the following methods into your PassportAuthController.php file:

Now we will create rest api crud methods in ProductController.php. So navigate to app/http/controllers/API directory and open ProductController.php file. And, update the following methods into your ProductController.php file:

Then open command prompt and run the following command to start developement server:

Test Laravel 8 REST CRUD API with Passport Auth in Postman

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

1 – Laravel Register Rest API :

Laravel 8 REST CRUD API with Passport Auth 1

2 – Login API :

Laravel 8 REST CRUD API with Passport Auth 2

Next Step, you will call getUser, create product, list product, edit product, and delete product APIs, In this apis need to pass the access token as headers:

3 – Product List API

  • Method:- GET
  • URL:- http://127.0.0.1:8000/api/products

4 – Product Create API

  • Method:- POST
  • URL:- http://127.0.0.1:8000/api/products

5 – Product Fetch API

  • Method:- GET
  • URL :- http://127.0.0.1:8000/api/products/{id}

6 – Product Update API

  • Method:- PUT
  • URL :- http://127.0.0.1:8000/api/products/{id}

7 – Product Delete API

  • Method:- DELETE
  • URL :- http://127.0.0.1:8000/api/products/{id}

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