Build RESTful API In Laravel Framework


To follow along with me, you need to have some basic understanding of Laravel. If you are new to Laravel, please refer to my other post Getting Started With Laravel.

What We Are Building

We will be building a CRUD (Create, Read, Update, Delete) API for a Book Management System. Our API Endpoints will be –

1. GET – api/books => will retrieve all the books stored in the database.
2. GET – api/books/{id} => will return a specific book.
3. POST – api/books => will create a new book record to the database.
4. PUT – api/books/{id} => will update an existing record.
5. DELETE – api/books/{id} => will delete a particular book from database.

Setup the Laravel Application

To get started you need to create a new Laravel Application. If you have installed Laravel in your system you will be able to utilize Laravel’s CLI commands. Type the following in your Terminal / Console.

λ laravel new crud-app

Next, change your current directory to the root folder of the project.

λ cd crud-app

To start the Laravel Server, type the following command in your Terminal / Console.

λ php artisan serve

You will be able to visit your application on

Now assuming that you have installed MySQL already, let's create a new database by typing the following commands to the Terminal / Console.

λ mysql -u root -p

λ mysql> CREATE DATABASE books-db

Note: You will be prompted for password after running mysql -u root -p

Now, first thing first – as you may know Laravel is a MVC (Model-View-Controller) Framework, we need to create a model first. We will name our model as “books” for this application.

λ php artisan make:model books

Alright then, now to store or retrieve data from database we must need a database table right ? Let’s make that right now, we will do that through Laravel’s Database Migration. Copy the following code to your Terminal / Console.

λ php artisan make:migration create_books_table

You will see a new migration file in
crud-app > database > migrations

If you are new to Laravel or unaware application's folder structure, please see my other post Getting Started With Laravel.

Now, we need to create one Controller, which will handle all the incoming requests. Do the following to create one.

λ php artisan make:controller BooksController

Note: We don't need to create View for this application.

Okay, with everything set and done, we need to modify all these newly created files one by one. Now open your project in Visual Studio Code or any other Code Editor of your choice.

Edit: crud-app / database / migration / create_books_table.php
Paste the following code inside of - public function up() and Save it.

After saving the following file, we need to migrate this to create a table with the following fields. To migrate do the following in your Terminal / Console (inside root directory of your project)

λ php artisan migrate

Once done, you will see "migrated successfully" message in your Console to read and write record from database table through Controller, we need to add few lines to the books model.

Edit: crud-app / app / books.php
Paste the following code inside of - class books extends Model

We are almost half way done with this, now we need to modify two more file api.php and BooksContoller.php. Let's do that real quick.

Edit: crud-app / routes / api.php

/** Books API */

Route::apiResource('/books', 'BooksController');

Note: apiResouce is taking two parameters, first one is the endpoint itself and second is the name of the Controller.

There are ton of methods to define API and Controllers into Laravel Route, here we used Laravel apiResource method to quickly create all the necessary endpoints for this application. You can read the apiResource documentation here.

After updating the api.php you can return to your Terminal / Console to check the routes created for this application by typing the following command.

λ php artisan route:list

You will see something like this, in your Terminal / Console.

Now you have reached almost end of this journey, we need to edit BooksController.php. But before that we need to generate a resource class and resourceCollection. In API, resource sits between App Model and JSON responses and works as a transformation layer between the two. Laravel’s resource classes allow you to expressively and easily transform your models and model collections into JSON.

To create resource and resourceCollection do the following in your Terminal / Console.

λ php artisan make:resource booksResource

λ php artisan make:resource bookResourceCollection

Note: We don't need to modify anything inside of booksResource or booksResourceCollection. However you will find them inside crud-app > app > Http > Resources

Edit: crud-app / app / Http / Controllers / BooksController.php
Paste the following code inside of - class BooksController extends Controller

With this, we completed our coding part. Now we need to test our API using Postman.

As we don't have any record in our database table, first we will try to create a book record using POST method.

If everything works fine you will see a JSON response in Postman indicating that one record is successfully created in the database.

To retrieve all the books stored in the table you can set Request Method to GET and URL to Similarly to retrieve any specific book by id pass on book id to the URL

With this I am wrapping up this tutorial. I will cover a lot of things on Laravel over time, please watch this space for upcoming updates and posts of Laravel.

You can download this complete tutorial from my GitHub Repository

Copy link
Powered by Social Snap