Laravel is one of the most popular PHP frameworks — and for good reason. Its elegant syntax, built-in tools, and vibrant community make it an excellent choice for building web applications, including RESTful APIs. In this tutorial, we’ll walk through how to build a basic REST API using Laravel, ideal for your startup's backend or MVP.
🔧 What You’ll Need
PHP 8.0 or higher
Composer
Laravel CLI
A database (MySQL or SQLite for local dev)
Postman or curl for testing
🛠 Step 1: Create a New Laravel Project
Open your terminal and run:
This will create a new Laravel project in the rest-api-demo
directory.
🗄 Step 2: Set Up the Database
Open .env
and configure your database:
Then create the database in MySQL.
📦 Step 3: Create a Model and Migration
Let’s say we’re building an API for managing tasks.
In the generated migration file (database/migrations/xxxx_xx_xx_create_tasks_table.php
), define the table schema:
Run the migration:
🧠 Step 4: Create a Controller
Generate a resource controller:
Open app/Http/Controllers/TaskController.php
and define basic CRUD methods:
🛣 Step 5: Define API Routes
Open routes/api.php
and add:
Laravel will automatically create all the typical RESTful routes: index
, store
, show
, update
, and destroy
.
✅ Step 6: Test Your API
Start the server:
Test with Postman or curl
:
🔐 (Optional) Add Authentication
Laravel offers API authentication out of the box with Laravel Sanctum or Passport. If your app needs auth, install Sanctum:
Then add HasApiTokens
to your User model and protect routes with middleware.
🧾 Final Thoughts
Laravel makes building APIs clean and efficient. This simple Task API is a great starting point, and you can scale it with features like validation, relationships, pagination, authentication, and more.
Perfect for MVPs, admin panels, or mobile backends — Laravel has your startup covered.