Laravel 8 Sanctum API Authentication Tutorial

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

In this Laravel 8 Sanctum API Authentication Tutorial I will show you how to create REST API for Simple CRUD application with Sanctum authentication In laravel. In this tutorial you will learn to create rest api for simple crud operation with Sanctum authentication In laravel 8 application. In this article I will share example to create 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 for product application.

Laravel 8 Sanctum API Authentication Tutorial

With this tutorial you learn to create fully functional restful API with sanctum authentication in Laravel 8. We will be creating fully functional REST API along with sanctum Authentication.

Step 1: Install Laravel 8

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

Make sure you have composer installed.

Setup Database Credentials

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.

Step 2: Use Sanctum

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

After installing package, we have to publish configuration file.

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

Now, add middleware for sanctum api.

app/Http/Kernel.php

Step 3: Sanctum Configuration

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

app/Models/User.php

Step 4: Add Product Table and Model

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

Now open migration file and update the function up() method as following:

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

Lets Create “products” table and then create a model file app/Models/Product.php and put following code in it:

app/Models/Product.php

Step 5: Create API Routes

Now we will need to define crud operation rest api resource routes along with auth routes. Go to the routes directory and open api.php. Then put the following routes into api.php file:

routes/api.php

Step 6: Create Controller Files

Now, lets create following controller files:

app/Http/Controllers/API/BaseController.php

app/Http/Controllers/API/RegisterController.php

app/Http/Controllers/API/ProductController.php

Step 7: Create Eloquent API Resources

Now create new api resource using following artisan command:

app/Http/Resources/Product.php

Run Development Server

Now we are ready to test crud operation restful api and sanctum auth api in laravel. Open command prompt and run the following command to start developement server:

make sure to use token with crud api

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