Posts

Showing posts from January, 2022

Laravel API With Sanctum

Image
  Laravel API With Sanctum Install a Laravel project           composer create-project laravel/laravel [my-project-name] Install the package           Composer require laravel/sanctum Do the migration           php artisan vendor:publish --provider=”Laravel\Sanctum\SanctumServiceProvider”  php artisan migrate Replace the kernel.php -> middlewareGroups -> api 'api' => [     \Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful::class,     'throttle:api',     \Illuminate\Routing\Middleware\SubstituteBindings::class, ], Add this to User model           use Laravel\Sanctum\HasApiTokens; class User extends Authenticatable { use HasApiTokens, HasFactory, Notifiable;} Add this to api.php routes           Route::group(['middleware' => ['auth:sanctum']], function () { Route::get('/employees/search/{name}', [EmployeeController::class, 'search']); }); Try it with postman (make sure you are defining the header) Separate route