Sitemap

How to Build a Simple REST API with Laravel 10 in 15 Minutes

2 min readMay 10, 2025

--

How to Build a Simple REST API with Laravel 10 in 15 Minutes

Introduction

Are you a beginner Laravel developer eager to build your first REST API? Laravel 10 makes it easier than ever to set up a fast, elegant, and powerful API with minimal effort.

In this step-by-step guide, you’ll learn how to create a simple RESTful API in just 15 minutes. By the end, you’ll be able to fetch, create, update, and delete data using Laravel’s built-in tools

Prerequisites

Before we start, make sure you have the following:

  • PHP >= 8.1
  • Composer
  • Laravel 10 installed
  • MySQL or SQLite
  • Postman (or any API testing tool)

Step 1: Create a New Laravel Project

Open your terminal and run:

composer create-project laravel/laravel laravel-api-demo

Navigate into the project:

cd laravel-api-demo

Step 2: Set Up the Database

Edit the .env file and configure your database

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=api_demo
DB_USERNAME=root
DB_PASSWORD=your_password

Then run:

php artisan migrate

Step 3: Create a Model and Migration

Let’s create a simple Post model:

php artisan make:model Post -m

In the migration file (database/migrations/..._create_posts_table.php), define the schema:

public function up(): void
{
Schema::create('posts', function (Blueprint $table) {
$table->id();
$table->string('title');
$table->text('body');
$table->timestamps();
});
}

Run the migration:

php artisan migrate

Step 4: Create a Controller

php artisan make:controller PostController --api

Inside PostController.php, add basic CRUD methods:

use App\Models\Post;
use Illuminate\Http\Request;

class PostController extends Controller
{
public function index() {
return Post::all();
}

public function store(Request $request) {
$validated = $request->validate([
'title' => 'required|string',
'body' => 'required|string',
]);

return Post::create($validated);
}

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::destroy($id);
return response()->json(['message' => 'Deleted']);
}
}

Step 5: Define API Routes

Open routes/api.php and add:

use App\Http\Controllers\PostController;

Route::apiResource('posts', PostController::class);

Now your API is ready to handle:

GET /api/posts — List all posts

POST /api/posts — Create a new post

GET /api/posts/{id} — Show a single post

PUT/PATCH /api/posts/{id} — Update a post

DELETE /api/posts/{id} — Delete a post

Step 6: Test Your API

Use Postman or Insomnia to test your endpoints.

Example POST request to /api/posts

{
"title": "Hello Laravel",
"body": "This is my first API post!"
}

Bonus: Add CORS Support

To allow cross-origin requests (e.g. from a frontend app):

Install Laravel CORS (already included in Laravel 10)

php artisan vendor:publish --tag="cors"

Then, configure config/cors.php to allow all origins (or specific ones).

Conclusion

You just built a fully functional REST API in Laravel 10 in under 15 minutes! This is a perfect foundation for more advanced features like authentication, pagination, and relationships.

If you liked this guide, consider following me for more hands-on Laravel tutorials

--

--

MasteryOfLaravel
MasteryOfLaravel

Written by MasteryOfLaravel

Laravel developer with over 13 years of experience. Writing to share advanced tips, real-world solutions, and backend mastery

No responses yet