Skip to content
  • About Us
  • Contact Us
  • Privacy Policy
  • Terms & Conditions
  • Site Map

XpertPhp

Expertphp Is The Best Tutorial For Beginners

  • Home
  • Javascript
    • Jquery
    • React JS
    • Angularjs
    • Angular
    • Nodejs
  • Codeigniter
  • Laravel
  • Contact Us
  • About Us
  • Live Demos

Home » Laravel » Laravel 7 Elasticsearch integration from scratch with example

Laravel 7 Elasticsearch Integration From Scratch With Example

Laravel 7 Elasticsearch integration from scratch with example

Posted on May 12, 2020February 14, 2021 By XpertPhp No Comments on Laravel 7 Elasticsearch integration from scratch with example

In this article, we will discuss how to integrate Elasticsearch from scratch with example in laravel 7.

Elasticsearch is a real-time distributed and multitenant-capable full-text search engine. it provides HTTP web interface and JSON documents.

Overview

Step 1: Install Elasticsearch

Step 2: Create Table using migration

Step 3: Install Package

Step 4: Add providers and aliases

Step 5: Create Route

Step 6: Create a Model and Controller

Step 7: Create Blade Files

Step 8: Run Our Laravel Application

Step 1: Install Elasticsearch

We are going to install Elasticsearch in our system, If you haven’t downloaded elastic search then you can follow here.

Step 2: Create Table using migration

Now, We need to create a migration. so we will below command using create the students table migration.

PHP
1
php artisan make:migration create_students_table --create=students

After complete migration. we need below changes in the database/migrations/create_students_table file.

PHP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<?php
 
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
 
class CreateStudentsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('students', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('first_name');
            $table->string('last_name');
            $table->text('address');
            $table->timestamps();
        });
    }
 
    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('students');
    }
}
?>

Run the below command. after the changes above file.

PHP
1
php artisan migrate

Step 3: Install Package

Now, We will install “elasticquent/elasticquent” package. so first open the composer.json file and add the below line.

PHP
1
"elasticquent/elasticquent": "dev-master"

Step 5: Add providers and aliases

We will add below providers and aliases in the “config/app.php” file.

PHP
1
2
3
4
5
6
7
8
'providers' => [
....
Elasticquent\ElasticquentServiceProvider::class,
],
'aliases' => [
....
'Es' => Elasticquent\ElasticquentElasticsearchFacade::class,
]

Now, We will generate a configuration file for elastic search using the following command.

1
php artisan vendor:publish --provider="Elasticquent\ElasticquentServiceProvider"

Step 6: Create Route

Add the following route code in the “routes/web.php” file.

PHP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?php
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
   // return view('welcome');
});
Route::get('studentSearch', '[email protected]');
Route::post('studentSearchCreate', '[email protected]');
?>

Step 7: Create a Model and Controller

Here below command help to create the controller and model.

PHP
1
2
php artisan make:controller StudentController
php artisan make:model Student

Student.php

PHP
1
2
3
4
5
6
7
8
9
10
11
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Student extends Model
{
    //
protected $fillable = [
        'first_name','last_name', 'address'
    ];
}
?>

StudentController.php

PHP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<?php
namespace App\Http\Controllers;
use App\Student;
use Illuminate\Http\Request;
use Response;
class StudentController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
      if($request->has('search')){
        $students= Student::search($request->input('search'))->toArray();
      }
        return view('student-search',compact('students'));
    }
    /**
     * create student.
     *
     * @return \Illuminate\Http\Response
     */
    public function create(Request $request)
    {
      $this->validate($request, [
               'first_name' => 'required',
               'last_name' => 'required',
               'address' => 'required',
        ]);
        $student= Student::create($request->all());
        $student->addToIndex();
        return redirect()->back();
    }
}
?>

Step 8: Create Blade Files

Finally, We will create student-search file in the “resources/views/” folder directory and paste the below code.

student-search.blade.php

PHP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
@extends('layouts.app')
@section('content')
<div class="row">
    <div class="col-md-8 col-md-offset-2">
        <h1 class="text-primary" style="text-align: center;">Laravel 7 Elasticsearch integration from scratch with example</h1>
    </div>
</div>
<div class="container">
  <div class="panel panel-primary">
    <div class="panel-heading">
      <div class="row">
      <div class="col-lg-6">
        {{ Form::open(array('method'=>'get','class'=>'')) }}
        <div class="input-group">
          <input name="search" value="{{ old('search') }}" type="text" class="form-control" placeholder="Search for...">
          <span class="input-group-btn">
            <button class="btn btn-default" type="submit">Go!</button>
          </span>
        </div><!-- /input-group -->
        {{ Form::close() }}
      </div><!-- /.col-lg-6 -->
    </div><!-- /.row -->
    </div>
    <div class="panel-body">
        
        <div class="row">
          <div class="col-lg-6">
            @if(!empty($students))
              @foreach($students as $key => $value)
                <h3 class="text-danger">{{ $value['first_name'] }}</h3>
                <p>{{ $value['last_name'] }}</p>
                <p>{{ $value['address'] }}</p>
              @endforeach
            @endif
          </div>
          <div class="col-lg-6">
            <div class="panel panel-default">
              <div class="panel-heading">
                Create New Student
              </div>
              <div class="panel-body">
                @if (count($errors) > 0)
                <div class="alert alert-danger">
                  <strong>Whoops!</strong> There were some problems with your input.<br><br>
                  <ul>
                    @foreach ($errors->all() as $error)
                      <li>{{ $error }}</li>
                    @endforeach
                  </ul>
                </div>
              @endif
                {{ Form::open(array('url' => 'StudentSearchCreate','autocomplete'=>'off')) }}
                  <div class="row">
                              <div class="col-xs-12 col-sm-12 col-md-12">
                                  <div class="form-group">
                                      <strong>First Name:</strong>
                                      {{ Form::text('first_name', null, array('placeholder' => 'First Name','class' => 'form-control')) }}
                                  </div>
                              </div>
                              <div class="col-xs-12 col-sm-12 col-md-12">
                                  <div class="form-group">
                                      <strong>Last Name:</strong>
                                      {{ Form::text('last_name', null, array('placeholder' => 'Last Name','class' => 'form-control')) }}
                                  </div>
                              </div>
                              <div class="col-xs-12 col-sm-12 col-md-12">
                                  <div class="form-group">
                                      <strong>Address:</strong>
                                      {{ Form::text('address', null, array('placeholder' => 'Address','class' => 'form-control')) }}
                                  </div>
                              </div>
                          </div>
                          <div class="text-center">
                            <button type="submit" class="btn btn-primary">Submit</button>
                          </div>
                {{ Form::close() }}
              </div>
            </div>
          </div>
        </div>
    </div>
  </div>
</div>
@endsection

Step 9: Run Our Laravel Application
We can start the server and run this example using the below command.

1
php artisan serve

Now we will run our example using the below Url in the browser.

1
http://127.0.0.1:8000/studentSearch

Please follow and like us:
error
fb-share-icon
Tweet
fb-share-icon

Recommended Posts:

  • laravel eloquent WhereIn query example
  • Laravel 8 Generate Dynamic XML Sitemap
  • Google Autocomplete Address example in Laravel 6
  • Laravel 7 Summernote Image Upload With Example
  • Laravel 8 S3 File Upload Tutorial With Example
Laravel Tags:elasticsearch in laravel, how to implement elasticsearch in laravel, how to install elasticsearch in laravel, how to use elasticsearch in laravel, laravel 7, laravel elasticsearch example

Post navigation

Previous Post: Laravel 7 GEO Chart Example using lavacharts
Next Post: laravel 7 redirect route with query string

Latest Posts

  • Laravel Check If Foreach is Empty Example
  • Laravel Check Array Empty in Blade
  • PHP Datatables CRUD Example
  • How to Convert Date and Time from one timezone to another in php
  • how to get current date and time in php
  • Drag and Drop Reorder Items with jQuery, PHP & MySQL
  • Laravel 9 Toastr Notifications Example Tutorial
  • Laravel 9 CRUD Operation Example Using Google Firebase
  • Laravel 9 CKeditor Image Upload With Example
  • Laravel 9 Summernote Image Upload With Example

Tools

  • Compound Interest Calculator
  • Hex to RGB Color Converter
  • Pinterest Video Downloader
  • Birthday Calculator
  • Convert JSON to PHP Array Online
  • JavaScript Minifier
  • CSS Beautifier
  • CSS Minifier
  • JSON Beautifier
  • JSON Minifier

Categories

  • Ajax
  • Angular
  • Angularjs
  • Bootstrap
  • Codeigniter
  • Css
  • Htaccess
  • Javascript
  • Jquery
  • Laravel
  • MongoDB
  • MySql
  • Nodejs
  • Php
  • React JS
  • Shopify Api
  • Ubuntu

Tags

angular 10 tutorial angular 11 ci tutorial codeigniter 4 image upload Codeigniter 4 Tutorial codeigniter tutorial CodeIgniter tutorial for beginners codeigniter with mysql crud operation eloquent relationships file upload File Validation form validation Image Upload jQuery Ajax Form Handling jquery tricks jquery tutorial laravel 6 Laravel 6 Eloquent Laravel 6 Model laravel 6 relationship laravel 6 relationship eloquent Laravel 6 Routing laravel 7 Laravel 7 Eloquent laravel 7 routing laravel 7 tutorial Laravel 8 laravel 8 example laravel 8 tutorial laravel 9 example laravel 9 tutorial Laravel Framework laravel from scratch Laravel Socialite laravel social login nodejs pagination payment gateway php with mysql react js tutorial rewrite rule send mail validation wysiwyg editor

Copyright © 2018 - 2022,

All Rights Reserved Powered by XpertPhp.com