Create REST API Example Using Lumen Micro Framework by Laravel -Part I

Rest Web service is very popular service to communicate between client and server.There are a lot of front-end JavaScript framework available which is using web services to communicate with server. Lumen-micro framework is fast and light weight api micro-framework by Laravel using php.
This Lumen tutorial help to create Add,edit and delete rest api call using Lumen. I am using Eloquent ORM to create model that help to communicate mysql with Lumen for database related operation.

I am creation own api rest end points which will insert record into mysql database and edit record on database.You can also delete record from mysql database using lumen rest call.

lumen-micro framework

We will gone through following points in this tutorial,

  1. Create Database connection in Lumen with MySQL
  2. Migrating table into database using Lumen
  3. Get records from mysql database table using Eloquent Model
  4. Create Add,edit and delete Rest API call

How to connect MySQL with Lumen

Its very easy and simple to connect database with lumen framework.Lumen had .env file which is use for set global level environment parameters for Lumen application. Please make sure do not upload this .env file on version control repository like GIT, SVN etc.

This file will contains all application level credential. You can also define mysql connection information into config/database.php file, but this file will available to all public users, as a security concern nobody wants to share connection information to any public users.We will open .env file which is located on route of your project test_lumen/.env.

As you can see mysql connection information,The parameters are,
DB_CONNECTION : This will contain database driver information,like mysql,oracle etc.I am using mysql.
DB_HOST : This will contain hostname of database like, IP address or hostname, if your lumen application code and mysql server on same machine.You can define localhost as a hostname.
DB_DATABASE : The name of database.You can replace as per your db name.
DB_USERNAME : The username of mysql database.
DB_PASSWORD : The password of mysql database.

How To Enable eloquent in Laravel/Lumen

The Eloquent is popular and popular ORM(Object-relational mapping), by default Lumen and laravel providing support for that, we just need to enable Eloquent by un-commenting line in test_lumen/bootstrap/app.php file.Each database table has a corresponding "Model" which is used to interact with that table. Models allow you to query for data in your tables, as well as insert new records,edit record and delete record from the table.

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

Migration

Migration is automated process of database integration with application.There are many CMS and framework using Migration to migrate database tables.We will create two table and both tables has associated records using foreign-key.

Above command will create two tables file under "test_lumen/database/migration" folder.You need to open those file and look into these file,You can see there are two methods already create one "up()" for create table schema and another is "down()" which will use drop the table at the time of rollback.

We will put below code into create_user_views_table file,This will create user_views table into database.

After make change in above file,The create_user_views_table file will look like below,

We will put below code into create_user_view_details_table file,This will create user_view_details table into database.

Now we will define referential integrity with first one table,We will add below php code before closing tag of "up()" method into create_user_view_details_table file,

Once you have done changes in create_user_view_details_table file the whole file code look like below,

So whenever you will run migrate command on any server this will create user_views and user_view_details tables into database, You don’t need manual interaction to create table into database.

The migrate command is :

php artisan migrate

Continue lumen rest api tutorial in next part II