How to Build a Simple REST API with Laravel in 2025


Learn how to create a clean and efficient REST API with Laravel in 2025 with this step-by-step guide, perfect for both beginners and experienced developers.
Building APIs is a fundamental task for modern web developers, and Laravel simplifies this process significantly. Whether you're developing a backend for a mobile app, a single-page application, or a microservice, Laravel's robust tools enable you to construct RESTful APIs swiftly and efficiently. In this post, I'll guide you through the process of creating a simple REST API using Laravel 10, the latest stable version in 2025.
In this post, I’ll walk you through creating a simple REST API using Laravel 10 (the latest stable version in 2025).
Step 1: Setting Up Your Laravel Project
First, make sure you have Composer installed. Then, create a new Laravel project:
composer create-project laravel/laravel laravel-api
cd laravel-api
Step 2: Create a Model and Migration
Let’s say we want to manage "Tasks." We can generate a model and migration with:
bashKopyalaDüzenlephp artisan make:model Task -m
Open the generated migration file (database/migrations/xxxx_xx_xx_create_tasks_table.php
) and add some fields:
phpKopyalaDüzenlepublic function up()
{
Schema::create('tasks', function (Blueprint $table) {
$table->id();
$table->string('title');
$table->boolean('completed')->default(false);
$table->timestamps();
});
}
Run the migration:
bashKopyalaDüzenlephp artisan migrate
Step 3: Create a Resource Controller
Generate a controller with resource methods:
bashKopyalaDüzenlephp artisan make:controller TaskController --api
Step 4: Define API Routes
Open routes/api.php
and add:
phpKopyalaDüzenleuse App\Http\Controllers\TaskController;
Route::apiResource('tasks', TaskController::class);
This creates all standard RESTful routes for the "tasks" resource.
Step 5: Implement Controller Methods
Open app/Http/Controllers/TaskController.php
and implement the basic CRUD:
phpKopyalaDüzenleuse App\Models\Task;
use Illuminate\Http\Request;
class TaskController extends Controller
{
public function index()
{
return Task::all();
}
public function store(Request $request)
{
$validated = $request->validate([
'title' => 'required|string|max:255',
'completed' => 'boolean',
]);
$task = Task::create($validated);
return response()->json($task, 201);
}
public function show(Task $task)
{
return $task;
}
public function update(Request $request, Task $task)
{
$validated = $request->validate([
'title' => 'sometimes|required|string|max:255',
'completed' => 'sometimes|boolean',
]);
$task->update($validated);
return response()->json($task);
}
public function destroy(Task $task)
{
$task->delete();
return response()->json(null, 204);
}
}
Don’t forget to allow mass assignment in the Task
model (app/Models/Task.php
):
phpKopyalaDüzenleprotected $fillable = ['title', 'completed'];
Step 6: Test Your API
You can now test your API endpoints using tools like Postman or curl:
GET /api/tasks
— List all tasksPOST /api/tasks
— Create a new taskGET /api/tasks/{id}
— Show a single taskPUT /api/tasks/{id}
— Update a taskDELETE /api/tasks/{id}
— Delete a task
Final Words
With just a few steps, Laravel allows you to build a fully functional REST API. Its clear syntax and built-in features save you a lot of time and effort.
Next, you might want to add authentication, validation, and pagination to make your API ready for production.
Happy coding!
Subscribe to my newsletter
Read articles from Osman Aras directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
