Build RESTful API CRUD in Laravel 11 with Best Practices

Laravel 11 simplifies building RESTful APIs. In this guide, you’ll learn how to create a CRUD (Create, Read, Update, Delete) application following best practices including Repository pattern, validation, and resource classes.


Step 1: Setting Up Laravel

composer create-project --prefer-dist laravel/laravel rest-api-crud

Step 2: Configure MySQL Database

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=your_database_name
DB_USERNAME=your_username
DB_PASSWORD=your_password

Step 3: Create the Product Model with Migration

php artisan make:model Product -a

Define the migration in database/migrations/...:

public function up(): void {
    Schema::create('products', function (Blueprint $table) {
        $table->id();
        $table->string('name');
        $table->string('details');
        $table->timestamps();
    });
}

Step 4: Create Product Repository Interface

php artisan make:interface /Interfaces/ProductRepositoryInterface
<?php

namespace AppInterfaces;

interface ProductRepositoryInterface {
    public function index();
    public function getById($id);
    public function store(array $data);
    public function update(array $data, $id);
    public function delete($id);
}

Step 5: Implement Product Repository

php artisan make:class /Repositories/ProductRepository
<?php

namespace AppRepository;

use AppModelsProduct;
use AppInterfacesProductRepositoryInterface;

class ProductRepository implements ProductRepositoryInterface {
    public function index() { return Product::all(); }
    public function getById($id) { return Product::findOrFail($id); }
    public function store(array $data) { return Product::create($data); }
    public function update(array $data, $id) { return Product::whereId($id)->update($data); }
    public function delete($id) { Product::destroy($id); }
}

Step 6: Bind Interface and Implementation

php artisan make:provider RepositoryServiceProvider
public function register(): void {
    $this->app->bind(ProductRepositoryInterface::class, ProductRepository::class);
}

Step 7: Request Validation

php artisan make:request StoreProductRequest
php artisan make:request UpdateProductRequest

Step 8: Create a Common API Response Class

php artisan make:class /Classes/ApiResponseClass

Step 9: Create Product Resource

php artisan make:resource ProductResource

Step 10: Implement the Product Controller

php artisan make:controller ProductController

Step 11: Define API Routes

php artisan install:api
Route::apiResource('/products', ProductController::class);

Conclusion

By following these steps, you’ve built a RESTful API CRUD application in Laravel 11 using best practices like the Repository pattern, request validation, and resource classes. This ensures your API is scalable, maintainable, and secure.

Implement these practices in your projects and take your Laravel API development to the next level.


Frequently Asked Questions

What is a RESTful API CRUD in Laravel?+
What is the Repository pattern in Laravel?+
How do I validate requests in Laravel 11 APIs?+
What is a Resource class in Laravel?+
How do I bind a repository interface to its implementation?+