CRUD Example Using Laravel 7 and ElasticSearch

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 records 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

Let’s create CRUD laravel API that will handle insert, update and delete elasticsearch operation. I have created ESController.php and EsService.php – This file will have all service methods that will communicate with 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

Let’s open ESController.php file and added getAllData() method to get all record.

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

We ll create a method into Controller file to insert a new record into ES database. Already, Added a entry into api. php file which are using POST HTTP request, we ll pass required data into post payload.

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

Let’s update a record into the elasticsearch database.We ll create a method into controller file that ll call service method to update record into the database.

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

Now, I will delete a record from the ElasticSearch database.We ll create a method into controller file that ll call service method to delete record from the database.

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

We have learn ElasticSearch integration with laravel into first part of this tutorial, This tutorial help to create CRUD operation for elastic-search using laravel 7.Created Rest API add a record, update a record and delete a record from elasticsearch using Laravel 7.