Simple Lumen API for Microservice

Taufan Fadhilah Iskandar
3 min readDec 3, 2020
Lumen micro-framework by Laravel

Hi guys, in this article we will build a simple project with Lumen to integrate with microservice. If you don’t know what Microservice is, you can read my article before.

Okay, let’s start with create a new project called property-api. After instalation finished, we next step is set up database stuff such as migrations, factory, seeders and models.

Migration, Factory and Seeder

Create a new migration file to create residences table.

Residence Migration

Second migration file to create houses table.

House Migration

After you finish with your migration file, you can migrate with php artisan migrate command. Next continue to create factory file. Factory file purpose is to create fake data or dummy data, so we no need to input dummy data manually.

Create your first factory file named ResidenceFactory.

Residence Factory

Second factory file named HouseFactory.

House Factory

Finish with factory files, next is insert the fake data into our database by seeder. First seeder file named ResidencesTableSeeder.

Residence Seeder

Second seeder file named HouseTableSeeder.

House Seeder

Last, call Residence and House Seeder in DatabaseSeeder.php .

Database Seeder

Before we seed the data into db, we need to set fillable field in our models. Residence Model should be like this.

Residence Model

For House Model can follow this.

House Model

Finally, use php artisan db:seed to insert our dummy data.

Controllers and Routes

This section we will create APIs, with Eloquent it help us to create an API so much!. First, we create API to get all residences with houses relation in ResidenceController.

Residence Controller

In HouseController, we create an API to get all house with residence relation. Of course we set the filter to prepare FE need filtered data.

House Controller

Last, we register the API in routes file

Routes File

Run the Lumen project with php -S localhost:8000 -t public.

Testing with Postman

Now we can check in Postman to make sure the data return as we expect.

Get All Residence API
Get All House API
Get All House with Filter API

From instructions above we already create APIs with Lumen. You can clone my project from my Github as well. Next, we continue to build User Service with Express Nodejs. See you!

--

--