Laravel 8 Sanctum Authentication CRUD REST API Tutorial

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

In this Laravel 8 Sanctum Authentication CRUD REST API Tutorial I will show you how to create CRUD REST API with Sanctum authentication In laravel. In this tutorial you will learn to create rest api for crud operation with Sanctum authentication In laravel 8 application. In this article I will share example to create 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. In this article we will be creating a task management crud operation rest api with sanctum authentication in laravel.

Laravel 8 Sanctum Authentication CRUD REST API Tutorial

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: Download Laravel App.
  • Step 2: Update Database Credentials.
  • Step 3: Add Laravel Sanctum.
  • Step 4: Add Table in Database.
  • Step 5: Make Laravel API Resources.
  • Step 6: Build Auth Controllers.
  • Step 7: Register New Routes.
  • Step 8: Test Laravel Auth APIs.

Download Laravel App

Update Database Credentials

Add Laravel Sanctum Library in Laravel

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

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


Now, add sanctum’s middleware to your api middleware group within your application’s 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.

Add Table in Database

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

After that provided command, place the suggested values into the app/Models/Task.php file.

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

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

Build API Resources

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

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

Build Auth Controller

In this step we will create some controller files as following:

app/Http/Controllers/API/BaseController.php
app/Http/Controllers/API/TaskController.php
app/Http/Controllers/API/AuthController.php

Now put the following code in app/Http/Controllers/API/BaseController.php.

Update app/Http/Controllers/API/TaskController.php as following:

Now put the following code in app/Http/Controllers/API/AuthController.php file.

Register New Routes

After this, we need to define authentication as well as crud routes for task management in “routes/api.php” file. Lets open “routes/api.php” file and add the following routes in it.

routes/api.php

Test Laravel Auth APIs

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

1) Register API: Verb:POST, URL:http://localhost:8000/api/register

2) Login API: Verb:POST, URL:http://localhost:8000/api/login.

Now, we can test the CRUD APIs, but before that, click to “Authorization” tab, select “Bearer Token” and define the token which you received after logging-in in the previous step.

3) Task List API: Verb:POST, URL:http://localhost:8000/api/tasks

4) Task Create API: Verb:POST, URL:http://localhost:8000/api/tasks

5) Task Show API: Verb:GET, URL:http://localhost:8000/api/tasks/{id}

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