Create REST API with the Laravel 5.3

This tutorial provides an example of building a complete RESTful API using Laravel 5.3.* 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.3.* Framework as first step. Before installing, Just want to give quick introduction about 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, lets start your journey-

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

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

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

Here “todo” is directory name all files will be downloaded into “todo” folder. Now move control to your project directory(todo) by issuing below command 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 lets add a table columns 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, Edit app/database/factories/ModelFactory.php and add

Create Task Model

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

Create a modal with

Run the Migrations

Below are the Laravel’s artisan commands, issue that commands from your project root. These commands will create those tables, and inset sample users and tasks to 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 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 TaskTransformer.php with below code

That’s it.Rest apis are ready now you can access the apis with below shown end points using advance reset client or Curl.

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.

I am Arjun from Hyderabad (India). I have been working as a software engineer from last 7+ years, and its my passion to learn new things and implement them as a practice. Aside from work, I likes gardening and spending time with pets.
  • Shany Mathew


  • Thank you, that was a precious article!

  • Sooraj J

    cannot work array of tasks

  • gaspan gurero

    how to use cacth it(json) in laravel again?

  • You have a typo i would say in TaskController, line 13.
    class TaskController extends Controller
    protected $respose;