Create REST API Example Using Lumen Micro Framework by Laravel – Part II

In first part of lumen rest tutorial, I have covered configuration of lumen framework.We have also learned how to connect with lumen with mysql. Created migration script using PHP artisan command and migrated table into database, Now We will gone through next part of this lumen rest tutorial.

We will create userviews and userviewdetails models file into app/Models/ folder, If you don’t find Models folder please create new one.We will create UserViews.php file and paste below code into them.

How to define Model into Lumen/laravel 5

Here we have define column name which will use to insert/edit records and will send data from server side.create_on and updated_on column value will automatically insert using current server time, you don’t need to send those col value to server.

We have create hasMany relation with UserViewDetails model to get records from user_wiew_details table.We have passed column name as well for foreign key raltion, where view_id column is foreign key into user_view_details table and id is primary key of user_views table.

We will create UserViewDetails.php file paste below code into this file.

Now we will create some routes into routes.php file which is located in app/Http folder.The Rest API end points are as follows,

as you can see, I have writen single line comment on each route which are describing rest call what they will do.

Now we will create UserViewsController.php file and write below code,

Get all records from Mysql Database Using Lumen and Eloquent

We will create new method getAllUserViews() into UserViewsController.php file and return results.This method will get all user views and corresponding details from userviews and userviewsdetails table.

Get Single record from Mysql Database Using Lumen and Eloquent

We will create new method getUserViewById() into UserViewsController.php file.This method will take view id as a parameters and return view information.I have use Lumen \Validator class to validate view id.

Create record into Mysql Database Using Lumen and Eloquent

We will create new method createUserView() into UserViewsController.php file.This method is post type and will create new record into MySQL.You need to send data into json format.

Update record into Mysql Database Using Lumen and Eloquent

We will create new POST type method saveUserView() into UserViewsController.php file.This method will update record into table to corresponding view id.You need to send data into json format from client side.

Delete record from Mysql Table Using Lumen and Eloquent

We will create Delete type request and new method deleteUserView() into UserViewsController.php file.This method will take view id as parameter and delete records from both tables.

Conclusion

I have create basic crud functionality of listing records table using Mysql,lumen and Eloquent ORM.You can add records and edit existing records into mysql database using rest webservice. You can also delete record from database table using ORM. These are basic Rest api for any front-end application which want to add,edit and delete records.You can integrate these rest call to any web application which are communicating to server using APIs.

You can download source code from below link.