A CRUD REST API in Laravel 11 allows users to perform CRUD (Create, Read, Update, Delete) operations on resources using RESTful principles.
Let’s start to create a RESTful API CRUD operation application with Laravel 11 using Laravel Passport auth:
Step 1 – Install Laravel and Create a New Project
Run the following composer command to install and create new laravel project:
composer create-project --prefer-dist laravel/laravel passport-auth-example
Step 2: Install Laravel Passport
Install passport auth to create rest api:
cd passport-auth-example
php artisan install:api --passport
Step 3 – Configure Passport
Edit your user.php
model from app/models folder, and add the HasApiTokens
trait:
use Laravel\Passport\HasApiTokens;
class User extends Authenticatable
{
use HasApiTokens, HasFactory, Notifiable;
// Rest of your model code...
}
Edit config/auth.php
file and API driver:
[
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'passport',
'provider' => 'users',
],
],
Step 4 – Create Model and Set Up Database
Create your models for perform crud operation from database:
php artisan make:model Post -m
Edit .env file and configure database details in it:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=here your database name here
DB_USERNAME=here database username here
DB_PASSWORD=here database password here
Run migration command to create tables into your configured database:
php artisan serve
Step 5 – Create CRUD and Auth API Routes
Next, define the routes for your API endpoints. Open your api.php
file located at routes/api.php
and define your routes:
use App\Http\Controllers\API\PassportAuthController;
use App\Http\Controllers\API\PostController;
Route::post('register', [PassportAuthController::class, 'register']);
Route::post('login', [PassportAuthController::class, 'login']);
Route::middleware('auth:api')->group(function () {
Route::get('get-user', [PassportAuthController::class, 'userInfo']);
Route::resource('posts', PostController::class);
});
Step 6 – Create Auth and CRUD Controller
Create a controller file by using the following command:
php artisan make:controller Api\PassportAuthController
php artisan make:controller API\PostController
Now implement authentication methods into it to handle login, registration and user detail functionality from database:
<?php
namespace App\Http\Controllers\API;
use Illuminate\Http\Request;
use App\Models\User;
class AuthController extends Controller
{
/**
* Registration Req
*/
public function register(Request $request)
{
$this->validate($request, [
'name' => 'required|min:4',
'email' => 'required|email',
'password' => 'required|min:8',
]);
$user = User::create([
'name' => $request->name,
'email' => $request->email,
'password' => bcrypt($request->password)
]);
$token = $user->createToken('PassportAuth')->accessToken;
return response()->json(['token' => $token], 200);
}
/**
* Login Req
*/
public function login(Request $request)
{
$data = [
'email' => $request->email,
'password' => $request->password
];
if (auth()->attempt($data)) {
$token = auth()->user()->createToken('PassportAuth')->accessToken;
return response()->json(['token' => $token], 200);
} else {
return response()->json(['error' => 'Unauthorised'], 401);
}
}
public function userInfo()
{
$user = auth()->user();
return response()->json(['user' => $user], 200);
}
}
Implement CRUD operations in your postcontroller.php
file.
namespace App\Http\Controllers\API;
use App\Models\Post;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
class PostController extends Controller
{
public function index()
{
return Post::all();
}
public function store(Request $request)
{
return Post::create($request->all());
}
public function show($id)
{
return Post::findOrFail($id);
}
public function update(Request $request, $id)
{
$post = Post::findOrFail($id);
$post->update($request->all());
return $post;
}
public function destroy($id)
{
$post = Post::findOrFail($id);
$post->delete();
return 204;
}
}
Step 7 – Test
Run artisan serve command to start the application server:
php artisan serve
Start Postman application and call these API for testing.