John Davidson

php - laravel sanctum won't create database table

0 comments
Message:


Currently I trying to make an API auth using laravel sanctum in every tutorial that I watched


while running php artisan migrate it created a new table with the name of create_personal_access_token_table but when I run php artisan migrate it says Nothing to migrate


Note:I have already created laravel auth and tables with bootstrap.


Laravel Version : 8.61.0



My Answer:
Laravel Sanctum is a package that provides a simple authentication system for SPAs (Single Page Applications), mobile applications, and token-based APIs in Laravel. It allows you to authenticate users using API tokens and provides a simple way to manage API tokens for your application.

To get started with Laravel Sanctum, you can follow these steps:

1. Install Laravel Sanctum using Composer:
bash
composer require laravel/sanctum


2. Publish the Sanctum configuration file:
bash
php artisan vendor:publish --provider="Laravel\Sanctum\SanctumServiceProvider"


3. Run the Sanctum migrations to create the necessary database tables:
bash
php artisan migrate


4. Add the Sanctum middleware to your API routes in `routes/api.php`:
php
use Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful;

Route::middleware('auth:sanctum')->get('/user', function (Request $request) {
return $request->user();
});


5. To authenticate users and generate API tokens, you can use the `Sanctum` facade in your controllers or routes:
php
use Laravel\Sanctum\Sanctum;

$user = User::find(1);
$token = $user->createToken('token-name')->plainTextToken;


6. You can also revoke tokens and manage user sessions using the `Sanctum` facade:
php
use Laravel\Sanctum\Sanctum;

$user->tokens()->delete();


These are just some basic steps to get started with Laravel Sanctum. You can refer to the official Laravel Sanctum documentation for more advanced usage and features.

Rate this post

3 of 5 based on 4556 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us