Live search in laravel using AJAX

11,125

You are including the results view in the form blade, where the results variable in the results view is not initially available. You could instead do a check in the results view to make sure to loop through the collection only if the data is available

<table style="width:100%">
    <tr>
        <th>Name</th>
        <th>Logo</th>
    </tr>
    @if (isset($results) && count($results) > 0)
    @foreach( $results as $business )
    <tr>
        <td>{{ $business->logo }}</td>
        <td>{{ $business->name}}</td>
    </tr>
    @endforeach
    @endif
</table>

EDIT

You are using Route type post and ajax method as get. You need to change your ajax method

function search_data(search_value) {
    $.ajax({
        url: '/searching/' + search_value,
        method: 'POST'
    }).done(function(response){
        $('#results').html(response); // put the returning html in the 'results' div
    });
}

and access that search text in your Controller as

public function search($search = null) {
    $search_text = $search;
    if ($search_text==NULL) {
        $data= Business::all();
    } else {
        $data=Business::where('name','LIKE', '%'.$search_text.'%')->get();
    }
    return view('results')->with('results',$data);
}
Share:
11,125
Przemek
Author by

Przemek

Updated on July 17, 2022

Comments

  • Przemek
    Przemek almost 2 years

    I want users to be able to search and display results on any page.

    Search box is in navbar like this:

    <form action="/search" method="get" autocomplete="off" class="navbar-form navbar-left">
        <div class="form-group">
            <input type="text" class="form-control" id="search_text" onkeyup="search_data(this.value, 'result'); placeholder="Search"></div>
            <div id="result">
                @include('results')
            </div>
        </div>
    </form>
    

    JQuery/AJAX:

    function search_data(search_value) {
        $.ajax({
            url: '/searching/' + search_value,
            method: 'GET'
        }).done(function(response){
            $('#results').html(response); // put the returning html in the 'results' div
        });
    }
    

    Controller:

    public function search() {
        $search_text = $_GET['text'];
        if ($search_text==NULL) {
            $data= Business::all();
        } else {
            $data=Business::where('name','LIKE', '%'.$search_text.'%')->get();
        }
        return view('results')->with('results',$data);
    }
    

    Route:

    Route::post('/searching', 'SearchController@search');
    

    Results.blade.php

    <table style="width:100%">
        <tr>
            <th>Name</th>
            <th>Logo</th>
        </tr>
        @foreach( $results as $business )
        <tr>
            <td>{{ $business->logo }}</td>
            <td>{{ $business->name}}</td>
        </tr>
        @endforeach
    </table>
    

    This is what I have got so far and I am getting this error:

    Undefined variable: results
    (View: C:\xampp\htdocs\laravel\resources\views\results.blade.php)
    (View: C:\xampp\htdocs\laravel\resources\views\results.blade.php)
    (View: C:\xampp\htdocs\laravel\resources\views\results.blade.php)

    Can anyone kindly please help a poor soul?