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

Rest Web service is a very popular service to communicate between client and server. There is a lot of front-end JavaScript framework available which is using web services to communicate with server.

Lumen-micro framework is fast and lightweight 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 a model that helps to communicate MySQL with Lumen for database-related operations.

I am creation my API rest endpoints which will insert a record into the MySQL database and edit a record on the database. You can also delete a record from the MySQL database using the lumen rest call.

You can also check other recommended tutorials of Lumen/Laravel,

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

It’s very easy and simple to connect the database with the 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 contain all application-level credentials. 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.

DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=test
DB_USERNAME=root
DB_PASSWORD=test123

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 a record and delete a 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.

$app->withFacades();
$app->withEloquent();

Migration

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

php artisan make:migration create_user_views_table –create=user_views
php artisan make:migration create_user_view_details_table –create=user_view_details

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.

Schema::create('user_views', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
	$table->string('created_by');
	$table->string('updated_by');
         $table->timestamps();
    });

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

<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateUserViewsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('user_views', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
			$table->string('created_by');
			$table->string('updated_by');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('user_views');
    }
}

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

Schema::create('user_view_details', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('view_id')->unsigned();
			$table->string('service_name');
			$table->string('created_by');
			$table->string('updated_by');
            $table->timestamps();
        });

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,

Schema::table('user_view_details', function($table) {
		   $table->foreign('view_id')->references('id')->on('user_views');
	   });

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

<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateUserViewDetailsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('user_view_details', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('view_id')->unsigned();
			$table->string('service_name');
			$table->string('created_by');
			$table->string('updated_by');
            $table->timestamps();
        });
		Schema::table('user_view_details', function($table) {
		   $table->foreign('view_id')->references('id')->on('user_views');
	   });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('user_view_details');
    }
}

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

Leave a Reply

Your email address will not be published.