Laravel 12 REST API Tutorial for Beginners (Step-by-Step Guide 2026)

  • 20-03-2026
  • 96
  • Laravel 12
  • Haresh Chauhan

Laravel 12 REST API Tutorial: In this guide, we will build a complete REST API using Laravel 12. REST APIs are widely used in modern web applications, especially in the USA, where frontend frameworks like React and mobile apps consume APIs.

This tutorial is beginner-friendly and will help you understand how to create a CRUD (Create, Read, Update, Delete) API step-by-step.

Step 1. Install Laravel 12

composer create-project laravel/laravel laravel-api

Navigate to your project:

cd laravel-api

Step 2. Setup Database

Update your .env file:

DB_DATABASE=laravel_api
DB_USERNAME=root
DB_PASSWORD=

Step 3. Create Model & Migration

php artisan make:model Post -m

Edit migration file:

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

Run migration:

php artisan migrate

Step 4. Create Controller

php artisan make:controller Api/PostController --api

Add the following code:

<?php

namespace App\Http\Controllers\Api;

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

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

    public function store(Request $request)
    {
        $post = Post::create($request->all());
        return response()->json($post, 201);
    }

    public function show($id)
    {
        return Post::findOrFail($id);
    }

    public function update(Request $request, $id)
    {
        $post = Post::findOrFail($id);
        $post->update($request->all());
        return response()->json($post);
    }

    public function destroy($id)
    {
        Post::destroy($id);
        return response()->json(['message' => 'Deleted']);
    }
}

Step 5. Add Fillable in Model

protected $fillable = ['title', 'description'];

Step 6. Create API Routes

Edit routes/api.php:

use App\Http\Controllers\Api\PostController;

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

Step 7. Test API

You can test your API using Postman:

  • GET /api/posts
  • POST /api/posts
  • GET /api/posts/{id}
  • PUT /api/posts/{id}
  • DELETE /api/posts/{id}

Conclusion

In this tutorial, we created a complete REST API in Laravel 12 with CRUD functionality. This is the foundation for building modern web and mobile applications.

In the next tutorial, we will integrate authentication using Laravel Sanctum.


We always thanks to you for reading our blogs.


dharmesh-image

Dharmesh Chauhan

(Swapinfoway Founder)

Hello Sir, We are brothers origin from Gujarat India, Fullstack developers working together since 2016. We have lots of skills in web development in different technologies here I mention PHP, Laravel, Javascript, Vuejs, Ajax, API, Payment Gateway Integration, Database, HTML5, CSS3, and Server Administration. So you need our service Please Contact Us

haresh-image

Haresh Chauhan

(Co-Founder)


We Are Also Recommending You :