How To Create Rest API Using Lumen 8

This RestApi tutorial will show you how to use Lumen with MySQL to develop a CRUD REST API. The rest API tutorial uses the Lumen micro-rest framework to read, add, update, and delete records.

I’m building a model with Eloquent ORM to enable Lumen to interact with MySQL for database operations.

Rest Web services are a popular way for clients and servers to connect. There are numerous front-end JavaScript frameworks that communicate with the server via web services. Lumen-micro framework is a PHP-based API micro-framework created by Laravel.

The Video Tutorial

Lumen CRUD Rest API Using MySQL

Let’s create a lumen project using composer and start a development environment for the lumen project.

Install Lumen by issuing the Composer create-project command in your terminal:

composer create-project --prefer-dist laravel/lumen customers

Start PHP development server:

php -S localhost:8000 -t public

In this tutorial, we’ll go over the following topics:

  • Create Database connection in Lumen with MySQL
  • Migrating table into database using Lumen
  • Get records from MySQL database table using Eloquent Model
  • Create Add,edit and delete Rest API call

How to connect MySQL with Lumen

The Lumen framework makes connecting to the database is very simple. The .env file in Lumen is used to set the application’s global level environment parameters. Please do not upload this .env file to a version control system such as GIT or SVN. All application-level credentials will be stored in this file.

Let’s update MySQL database configuration into .env file:

The parameters are:

DB_HOST: The database hostname.
DB_DATABASE: Contains database name.
DB_USERNAME: The username of MySQL database.
DB_PASSWORD: The password of MySQL database.

Create ‘test’ database into the mysql server and run below query to create ‘customers’ table :

You need to uncomment the below lines in the test_lumen/bootstrap/app.php file, if already un-commented below lines, please skip this step.

How to define Model into Lumen 8

A Model helps to query data to and from the table in the database. Lumen is using Eloquent ORM (Object Relational Mapping) to interact with the table. Let’s create a Eloquent model file 'Customer.php' under app/Models/ folder.

in the above model file, We have defined MySQL table field names that will use to get, insert and edit records.

Lumen API Routes

We’ll construct CRUD operation routes in the lumen framework’s routes/web.php file for application routes configuration.

Action Method in Laravel 8

Let’s get started by making a CustomerController.php file. This file is in charge of all action methods, including getting all records, getting a single record, adding a new record, updating an existing record, and deleting an existing record.

Get All Records from MySQL Table

We’ve put all of the essential helper classes and customer models at the front of the project. Using lumen and the Eloquent ORM all() method, I created an all() method to get entries from a MySQL database.

Get a Record from MySQL Table

Using the elegant eloquent() method, you can get a single record from MySQL. I’ve supplied $id as a parameter, which indicates which record to get.

Create a Record from MySQL Table

We’ll use the lumen Validator class to validate data and pass payloads as a parameter. We’ll use the eloquent create a function to insert the record after successful validation.

Update an Existing Record Using Lumen 8

Validate payloads and check whether or not a record exists in the MySQL database table, then update the record in the MySQL table.

Delete a Record from MySQL Table Using Lumen 8

We’ll pass the ‘id’ of the target record as a parameter, check whether the record exists in the customers table, and then delete the record from the MySQL table. For the no record found exception, ModelNotFoundException was created.


We have created a lumen project and added CRUD operations using the rest API endpoints. The rest API help to get all records, add a record, update a record and delete a record.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.