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:

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

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:

DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=rest_api_demo DB_USERNAME=root DB_PASSWORD=yourpassword

Then create the database in MySQL.


📦 Step 3: Create a Model and Migration

Let’s say we’re building an API for managing tasks.

php artisan make:model Task -m

In the generated migration file (database/migrations/xxxx_xx_xx_create_tasks_table.php), define the table schema:

public function up() { Schema::create('tasks', function (Blueprint $table) { $table->id(); $table->string('title'); $table->text('description')->nullable(); $table->boolean('completed')->default(false); $table->timestamps(); }); }

Run the migration:

php artisan migrate

🧠 Step 4: Create a Controller

Generate a resource controller:

php artisan make:controller TaskController --api

Open app/Http/Controllers/TaskController.php and define basic CRUD methods:

use 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', 'description' => 'nullable|string', ]); return Task::create($validated); } public function show(Task $task) { return $task; } public function update(Request $request, Task $task) { $validated = $request->validate([ 'title' => 'sometimes|string|max:255', 'description' => 'nullable|string', 'completed' => 'boolean', ]); $task->update($validated); return $task; } public function destroy(Task $task) { $task->delete(); return response()->noContent(); } }

🛣 Step 5: Define API Routes

Open routes/api.php and add:

use App\Http\Controllers\TaskController; Route::apiResource('tasks', TaskController::class);

Laravel will automatically create all the typical RESTful routes: index, store, show, update, and destroy.


✅ Step 6: Test Your API

Start the server:

php artisan serve

Test with Postman or curl:

curl http://127.0.0.1:8000/api/tasks

🔐 (Optional) Add Authentication

Laravel offers API authentication out of the box with Laravel Sanctum or Passport. If your app needs auth, install Sanctum:

composer require laravel/sanctum
php artisan vendor:publish --provider="Laravel\Sanctum\SanctumServiceProvider" php artisan migrate

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.