Day 13 – Building a REST API in Laravel Step-by-Step

Introduction

On Day 13, we dive into building a RESTful API in Laravel, a crucial skill for modern web development. REST APIs allow applications to communicate over HTTP, enabling frontend frameworks like Vue.js or React to interact with a Laravel backend seamlessly.

At CuriosityTech.in, learners gain hands-on experience in creating APIs for CRUD operations, authentication, and real-world applications, ensuring they’re ready for professional full-stack development.


1. What is a REST API?

REST (Representational State Transfer) is an architectural style for designing web services.

ConceptDescription
EndpointURL through which clients interact with the API
MethodHTTP verbs: GET, POST, PUT, DELETE
RequestClient sends data or fetches resources
ResponseServer returns data in JSON format

2. Setting Up Laravel for API Development

Step 1: Create a Laravel project (if not already created):

composer create-project –prefer-dist laravel/laravel curiosity_api

cd curiosity_api

php artisan serve

Step 2: Configure database in .env:

DB_CONNECTION=mysql

DB_HOST=127.0.0.1

DB_PORT=3306

DB_DATABASE=curiosity_db

DB_USERNAME=root

DB_PASSWORD=


3. Creating a Model and Migration

php artisan make:model Product -m

Migration file (database/migrations/…_create_products_table.php):

public function up()

{

    Schema::create(‘products’, function (Blueprint $table) {

        $table->id();

        $table->string(‘name’);

        $table->decimal(‘price’, 8, 2);

        $table->timestamps();

    });

}

Run migration:

php artisan migrate


4. Creating API Routes

File: routes/api.php

use App\Http\Controllers\ProductController;

Route::get(‘/products’, [ProductController::class, ‘index’]);

Route::post(‘/products’, [ProductController::class, ‘store’]);

Route::get(‘/products/{id}’, [ProductController::class, ‘show’]);

Route::put(‘/products/{id}’, [ProductController::class, ‘update’]);

Route::delete(‘/products/{id}’, [ProductController::class, ‘destroy’]);


5. Building the Controller

php artisan make:controller ProductController –api

Controller (app/Http/Controllers/ProductController.php):

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Models\Product;

class ProductController extends Controller

{

    public function index() {

        return response()->json(Product::all());

    }

    public function store(Request $request) {

        $product = Product::create($request->all());

        return response()->json($product, 201);

    }

    public function show($id) {

        return response()->json(Product::find($id));

    }

    public function update(Request $request, $id) {

        $product = Product::find($id);

        $product->update($request->all());

        return response()->json($product);

    }

    public function destroy($id) {

        Product::destroy($id);

        return response()->json(null, 204);

    }

}


6. Testing the API

Use tools like Postman or cURL:

HTTP MethodEndpointAction
GET/api/productsFetch all products
POST/api/productsCreate a new product
GET/api/products/{id}Fetch single product
PUT/api/products/{id}Update product
DELETE/api/products/{id}Delete product

7. Hierarchical Diagram: REST API Workflow


8. CuriosityTech.in Perspective

At CuriosityTech.in, students learn to build real REST APIs integrated with Laravel backend. By combining CRUD operations, authentication, and MVC structure, learners create scalable, maintainable, and secure web services that can power modern applications.

Practical projects like online stores, task management apps, and dashboards are included to ensure hands-on learning.


9. Infographic Concept: REST API Steps in Laravel

This visual helps learners understand end-to-end API development in Laravel.


Conclusion

Building a REST API in Laravel is a key skill for Full Stack Developers. Today’s lesson equips developers to create robust, scalable, and secure backend services that can interact with frontend frameworks or mobile apps. Mastery of REST APIs sets the foundation for frontend integration (Day 15) and advanced Laravel projects.


Leave a Comment

Your email address will not be published. Required fields are marked *