This guide will walk you through the steps of creating a RESTful API in Laravel, from generating the necessary components to applying the Repository Pattern for data access abstraction.Prerequisites#
Laravel installed on your development machine.
Basic understanding of Laravel's MVC architecture.
Familiarity with RESTful principles.
Step 1: Create a Model and Migration#
Start by creating a model for your resource. For example, if you're creating a task management system, you'll need a Task model.This command creates a model file in app/Models/Task.php and a migration file in database/migrations/ for the tasks table.Step 2: Define the Migration#
Open the generated migration file and define the structure of your tasks table.Run the migration to create the table:Step 3: Create a Resource Controller
Generate a controller for your resource with CRUD operations predefined by Laravel.This command creates a controller under app/Http/Controllers/Resource/TaskController.php with methods for index, show, store, update, and destroy.Step 4: Create a Factory#
This will allow use to test your model later on or create database seeds.Here's how it would look like:Step 5: Create Repository#
Create a repository by extending your repository class to app/Repositories/Repository.php.Override the necessary methods as you may need, such as find, filter, create, paginate, update, and delete.Why create a repository?#
Repository is a layer between your controller and your model. This is where you put custom business logic needed between them, such as adding additional database column information, or request input transformation requirements.It should be designed to be reusable in cases you need to create separate controller for other needs, and void having to write the same code. Therefore this is to eradicate code redundancy within the project.Step 6: Create Resource#
Create a JsonResource class for Task by simply executing this command:Then customize as needed.Step 7: Create Request Class#
Create the needed request classes to be used on your controller. You should place them on a directory related to the Model it is intended to, for example: app/Http/Requests/Task/RetrieveTasksRequest.phpYou have to create request class for each request types:app/Http/Requests/Task/IndexRequest.php
app/Http/Requests/Task/ShowRequest.php
app/Http/Requests/Task/StoreRequest.php
app/Http/Requests/Task/UpdateRequest.php
app/Http/Requests/Task/DestroyRequest.php
Why do need a request class?#
This is to control the information that goes through the API endpoint and avoid SQL injections, brute force or any harmful data that could reach to your business logic. It has to be created individually for us to apply authorization logic on each type of request such as Role Based Access and Control.Step 7: Update the Controller#
We will now use the repository to the controller:As you can see on the example, we are using the repository to request or query tasks. The controller doesn't have to know how to query it as it should only serve as the handler whenever there's an HTTP Request.Then we used the TaskResource as return type, this will ensure we return JSON and have the controller what data should be return to the client.Step 8: Define Routes#
We can use the Route::apiResource method from laravel to automatically register all request types:Edit the file routes/api.php and add the following code:Step 9: Writing Test Scripts#
Using Pest, you must write API request tests within tests/Feature/Resource, on this case we will create tests/Feature/Resource/TaskTest.phpRun your test#
To make sure your endpoint runs correctly, you can run this very specific test by following this command:To run all tests and ensuring nothing breaks within the application: Modified at 2026-02-26 23:41:35