Create REST API with the Laravel 5.5

This tutorial provides an example of building a complete RESTful API using Laravel 5.5.* Framework. You gonna use the different HTTP methods during the REST API development, quick introduction about each method.

GET to retrieve data
POST to add data
PUT to update data
DELETE to delete data

Each method as a purpose isn’t it?. Let’s download and install Laravel 5.5.* Framework as the first step. Before installing, Just want to give a quick introduction to the app which you are going to build is TODO application. You can search for todos, add a todo to list, update and delete todos. Exciting right, let’s start your journey-

Create REST applications with the Laravel framework

STEP – 1 (Download and install Laravel 5.5.* Framework)

You can setup project with latest version by issue following command from your terminal.

If you want to setup project with a specific version, issue following command from your terminal.

Here, “todo” is project name, above command create a directory with project name and downloads all files inside it. Now move control to your project directory(todo) by issuing below command and serve application by using artisan command.

It will show http://localhost:8000/ URL if it is up successfully.

That’s about installation and congratulation, you have finished step one successfully. Let’s create make database configurations.

Configure Your Database

Create a database and edit .env and fill it with your database settings. This article assumes a MySQL database.

Create Migration File

Below command set up the basic migration scripts that we’ll be using to create the database table.

Now required migration file is in place, so let’s add a table column as shown below.

Edit app/database/migrations/SOME_DATE_create_tasks_table.php and edit and update the up() and down() methods:

Add Sample Users and todos

We gonna use Laravel’s seeds to create a few sample users and todos.

Let issue following commands to create seeder class from your root of the project.

Generated seeders by the above commands will be placed in the database/seeders directory. In our case, we have two seed files called UsersTableSeeder.php and TasksTableSeeder.php.

Update seeder run methods as shown below


Next, make sure that seeder class gets run when the database is seeded. Edit app/database/seeds/DatabaseSeeder.php

Add Model Factories

Next, create app/database/factories/UserFactory.php and app/database/factories/TaskFactory.php and place below code blocks

Create Task Model

Laravel comes with a User model setup, so let’s create a model for our tasks table.

Create a modal with

since you are following Laravel naming conventions you don’t have to change anything in the task model.

Run the Migrations

Below are the Laravel’s artisan commands and issue below commands from your project root. These commands will create those tables and inset sample users and tasks to the database.


When building an API it is common for people to just grab stuff from the database and pass it to json_encode(). This might be passable for “trivial” APIs but if they are in use by the public, or used by mobile applications then this will quickly lead to inconsistent output. So to avoid this problems we gonna use ellipsesynergie/api-response package. This package handles the response properly in your API and this package uses Fractal.

Fractal – Fractal provides a presentation and transformation layer for complex data output, the like found in RESTful APIs, and works really well with JSON. Think of this as a view layer for your JSON/YAML/etc.

Install package with the composer, issue the command from your root of the project:

Add this following service provider to your config/app.php file.

Create Task Controller

Now define your api routes in routes/api.php

Lets update task controller with below code, please read comments for better understanding.

Create a Transformer folder in your app directory and create a file called app\Transformer\TaskTransformer.php with below code

That’s it.Rest APIs are ready now you can access the APIs with below-shown endpoints using advance reset client or Curl.

Start the server with php artisan serve or use other other methods.

I hope you like this Post, Please feel free to comment below, your suggestion and problems if you face - we are here to solve your problems. Protection Status