Laravel 9 Crud Rest Api with Passport Auth

In this tutorial you will learn about the Laravel 9 Crud Rest Api with Passport Auth and its application with practical example.

In this Laravel 9 CRUD Api with Passport Auth Tutorial I will show you how to create CRUD REST API with passport authentication In laravel 9. In this tutorial you will learn to create crud and authentication rest api using passport authentication In laravel 9 application. In this article I will share example to create rest api for crud operations and user authentication in laravel 9. We will be creating simple crud operation application with user authentication. We will be using passport authentication package in this example. In this example you will learn how to install passport authentication package in laravel 9. After installing and configure passport authentication in laravel.

Laravel 9 User Authentication API

In this example we will be creating user authentication api means we will be creating rest api for user registration, login and to get user info. We will be using passport authentication for creating rest api for user authentication.

Laravel 9 CRUD REST API

We will be creating simple product crud operation application with user authentication. In this article, you will learn how to create fully functional CRUD (Create, Read, Update and Delete) REST API in laravel 9.

Laravel 9 Crud Rest Api with Passport Auth

In this step by step tutorial I will guide you through create a fully functional CRUD API with passport authentication in Laravel 9. Please follow the instruction given below:

  1. Install Laravel 9
  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 Laravel 9

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

Configure 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.

After successfully install laravel passport, register providers. Open config/app.php . Put the bellow code in it:

config/app.php

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

Passport Configuration

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

app/Models/User.php

go to  app/Providers/AuthServiceProvider.php file and register the registerPolicies() method inside the boot() function.

config/auth.php

Create Product Table and Model

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 create_products_table.php migration file and update the function up() method as following:

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

Run Migration

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

Create Passport Auth API 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/Api/ directory. Open the PassportAuthController.php file and put the following code in it.

app/Http/Controllers/Api/PassportAuthController.php

Create Product CRUD API Controller

Next we will create product crud api controller named ProductController using command given below –

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

app/Http/Controllers/Api/ProductController.php

Define API Routes

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

routes/api.php

Start Development Server

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

Laravel 9 User Registration API:

Laravel 9 User Login API:

Laravel 9 Get User Info. API:


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

Product List API

Product Create API

Method:- POST

Product Fetch API

Method:- GET

Product Update API

Method:- PUT

Product Delete API

Method:- DELETE

In this tutorial we have learn about the Laravel 9 Crud Rest Api with Passport Auth and its application with practical example. I hope you will like this tutorial.