CRUD Example Using Laravel 7 and ElasticSearch — Phpflow.com

This laravel 7 tutorial help to create CRUD rest end point using Elastic Search.We ll create rest API for add a record, edit a record , list all records and delete a record s from ES database.

Now days, web application/tools are creating on two layer, one is UI — which are creating on front-end technology react, angular etc and other is back-end which are creating on backend technology like nodejs, PHP, java etc.

I have created front-end into reactjs and backend API is creating into Laravel 7.The ElasticSearch is used as an database for this project. You can create front-end into any technology as per your project requirements.

I have already shared tutorials How To Integrate ElasticSearch With Laravel 7.I am extending this tutorial and adding CRUD operation using rest api.

Laravel CRUD API Using ElasticSearch

Open api.php file and added below entry for CRUD Operation -

I have created all rest end points.

How To Get All Record From Elastic Search

We have passed constant from and size parameter to service method, I have created all() method into EsService.php file.

How To Create A Record Into ElasticSearch Using Laravel

Added below method into the ESController.php file -

We have get all posted payloads data using $request->json()->all() method and send to the service create() method.Now added create() method into service file.

Update A Record Into ElasticSearch Using Laravel

as per route entry, Created a PUT type HTTP request to update data, we will pass ES id into the rest end point and post data into the HTTP request body as a json format.

Let’s add a below method into the ESController.php file -

This method takes $id- ES database id and payloads as parameter.We will create update() method into service.

We have get all posted payloads data using $request->json()->all() method and send to the service create() method. Now added update() method into service file.

Delete A Record from ElasticSearch Using Laravel

Let’s create a PUT type HTTP request to delete data, We will pass ES id into the rest end point.

Let’s add a below method into the ESController.php file -

This method takes $id- ES database id and payloads as parameter.We will create update() method into service.

I have ES id as parameter for delete a record using delete() method.

Conclusion

Originally published at https://www.phpflow.com on July 11, 2020.

Hey, I am Parvez Alam. A software developer since 2009. I am a programming addict. I love learning and sharing knowledge. https://www.phpflow.com/