Build Secure PHP REST API in Laravel 8 with Sanctum Auth

In this tutorial you will learn about the Build Secure PHP REST API in Laravel 8 with Sanctum Auth and its application with practical example.

In this Build Secure PHP REST API in Laravel 8 with Sanctum Auth Tutorial I will show you how to create REST API with Sanctum authentication In laravel. In this tutorial you will learn to create rest api with Sanctum authentication In laravel 8 application. In this article I will share example to create simple crud rest api with sanctum authentication in laravel. I will also show you how to install sanctum auth package in laravel. After installing and configure sanctum authentication in laravel we will create simple crud operation rest api.

Build Secure PHP REST API in Laravel 8 with Sanctum Auth

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

  • Step 1: Create Laravel Project
  • Step 2: Add Database Details
  • Step 3: Install Laravel Sanctum Pacakage
  • Step 4: Setting Up Sanctum
  • Step 5: Update Model and Run Migration
  • Step 6: Build API Resources
  • Step 7: Set Up Controllers
  • Step 8: Create REST API Routes
  • Step 09: Test REST API in Postman

Create Laravel Project

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

Add Database Details in ENV

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 Laravel Sanctum Pacakage

In this step, we will install Sanctum Package via the composer dependency manager. Use the following command to install Sanctum Package.

Setting Up Sanctum

Now, we need to publish the Sanctum configuration and migration files using the Artisan command.

Thereafter, register the sanctum middleware into the api array inside the app/Http/Kernel.php file

Now, run following command to migrate database schema along with sanctum tables in our database.


Lets Import the sanctum HasApiTokens service within the app/Models/User.php. Open App/User.php model file and add ‘Laravel\Sanctum\HasApiTokens’ trait in it.

Update Model and Run Migration

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

Open and add code into the database/migrations/create_blogs_table.php file. Add few properties into the migration file as following.

Next, create an app/Models/Blog.php file and register product migration properties inside the $fillable array.

Now, run following command to migrate database schema.

Build API Resources

In this step we will create Blog resource api using following command:

Now, go to app/Http/Resources/Blog.php and replace with the provided code.

Setting Up Controllers

In this we will update controller files as following. Open app/Http/Controllers/API/BaseController.php file and put the following code in it.

Open and place all the suggested code into the app/Http/Controllers/API/AuthController.php file.

Now go to the app/Http/Controllers/API/BlogController.php file and insert the CRUD operations code into it:

Create REST API Routes

After this, we need to define routes in “routes/api.php” file. Lets open “routes/api.php” file and add the following routes in it.

routes/api.php

Test Sanctum REST API in Postman

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

Test Register REST API

Test Login API

Create Post with Sanctum API

Get Single Post

Fetch All Posts

Update Post

Delete Record

In this tutorial we have learn about the Build Secure PHP REST API in Laravel 8 with Sanctum Auth and its application with practical example. I hope you will like this tutorial.