Simple Lumen API for Microservice
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.
Second migration file to create houses table.
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.
Second factory file named HouseFactory.
Finish with factory files, next is insert the fake data into our database by seeder. First seeder file named ResidencesTableSeeder.
Second seeder file named HouseTableSeeder.
Last, call Residence and House Seeder in DatabaseSeeder.php .
Before we seed the data into db, we need to set fillable field in our models. Residence Model should be like this.
For House Model can follow this.
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.
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.
Last, we register the API in 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.