1

Hello I can do a standard AJAX/PHP search but finding it hard to convert to Laravel. I'm using key up instead of button click. I'm not sure if this is the right way im going about to implement an ajax/laravel search bar. I'm looking to output the database data into the div in the view page, but need help on going about this. If anyone thinks im doing this wrong please advise me. Always willing to learn new code.

Controller:

<?php

namespace App\Http\Controllers;

use App\Patient;

use DB;

use Illuminate\Http\Request;

class PatientController extends Controller
{


public function search(Request $request) {
    // get the search term
    $text = $request->input('text');

    // search the members table
    $patients = DB::table('patients')->where('firstname', 'Like', $text)->get();


    // return the results
    return response()->json($patients);
}

}  

Route:

Route::get('search', 'PatientController@search');

View:

   @extends('Layout.master')


@section('content')


  <!-- Ajax code -->

       <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>


    <script type="application/javascript">
    $(document).ready(function(){

        $('#txtSearch').on('keyup', function(){

            var text = $('#txtSearch').val();

            $.ajax({

                type:"GET",
                url: '127.0.0.1:8000/search',
                data: {text: $('#txtSearch').val()},
                success: function(data) {

                    console.log(data);

                 }



            });


        });

    });
    </script>

    <div style="margin-top:70px;"></div>


       @include('partials._side')

    <div class="container">

                     <form method="get" action="">

                    <div class="input-group stylish-input-group">
                        <input type="text" id="txtSearch" name="txtSearch" class="form-control"  placeholder="Search..." >
                        <span class="input-group-addon">
                            <button type="submit">
                                <span class="glyphicon glyphicon-search"></span>
                            </button>  
                        </span>
                    </div>

                     </form> 


          <div id="result"></div>

</div>

@endsection
steven
  • 191
  • 2
  • 4
  • 12

3 Answers3

1

In your AJAX request change:

'127.0.0.1:8000/search'

to

'/search'

I suggest you to use jQuery Autocomplete

Very easy to set up.

Check out the API documentation.

meda
  • 45,103
  • 14
  • 92
  • 122
1

Depending on your version of Laravel (new versions use this):

use Illuminate\Http\Request;

public function search(Request $request) {
    $text = $request->input('text');

    $patients = DB::table('patients')->where('firstname', 'Like', "$text")->get();

    return response()->json($patients);
}

And then in javascript:

$(document).ready(function(){

    $('#txtSearch').on('keyup', function(){

        var text = $('#txtSearch').val();

        $.ajax({

            type:"GET",
            url: 'search',
            data: {text: $('#txtSearch').val()},
            success: function(response) {
                 response = JSON.parse(response);
                 for (var patient of response) {
                     console.log(patient);
                 }
             }



        });


    });

});

https://laravel.com/docs/5.1/requests#retrieving-input.

Matt Altepeter
  • 956
  • 1
  • 8
  • 18
  • Comments are not for extended discussion; this conversation has been [moved to chat](http://chat.stackoverflow.com/rooms/133685/discussion-on-answer-by-matt-altepeter-implement-laravel-ajax-search-bar). – user229044 Jan 21 '17 at 00:16
0

If you mean handle result in browser, please check inside your AJAX coded with jQuery, "response" variable is the server coming back data. You just need to populate the returned data into your page after AJAX finished.

For example put into

<div id="searchResult"></div>

You need put below inside your ajax after response coming back with jquery like:

$("#searchResult").html(response);
Mr Hoelee
  • 446
  • 2
  • 7
  • 24