1

I already have a functional search box form my website, but i want to implement a auto complete functionality for my search bar. I know there are a bunch of plugins like typeahead, and etc. I would like to implement typeahead or the jQuery Autocomplete widget, if you guys know how. I seen some tutorials, but they don't work for the code i have. I will show you the blade, controller and route that I have for my search box.

How can I load all my data from my database into the source field in the jQuery plugin? I would want to load a flyers title for example?

show.blade.php:

@extends('home')

@section('content')
        <div id="the-basics">
            <input class="typeahead" type="text" placeholder="Title">
        </div>
@stop

@section('scripts.footer')
    <script type="application/javascript" src="{{ URL::asset('/src/public/js/typeahead.js') }}"></script>
    <script>
        $('#the-basics .typeahead').typeahead({
                    hint: true,
                    highlight: true,
                    minLength: 1
                },
                {
                    name: 'title',
                    source: 'travelflyers/search'
                });

    </script>

@stop

TravelFlyersController.php:

class TravelFlyersController extends Controller {

    // Other functions here...

     public function search() {
       $keyword = Input::get('keyword');
       $flyers = Flyer::where('title', 'LIKE', '%' .$keyword. '%')->get(); 
       return \Response::json($flyers);
     }


}

Route:

Route::group(['middleware' => ['web']], function () {

    /** Resource Route For Travel Flyers */
    Route::resource('travelflyers', 'TravelFlyersController');


    Route::post('travelflyers/search',[
        'uses' => '\App\Http\Controllers\TravelFlyersController@search',
        'as'   => 'travelflyers.search',
    ]);


});
David
  • 1,987
  • 5
  • 33
  • 65

4 Answers4

0

Try this in your Routes File use GET instead of POST

Route::get('travelflyers/search',[
    'uses' => '\App\Http\Controllers\TravelFlyersController@search',
    'as'   => 'travelflyers.search',
]);
Dov Benyomin Sohacheski
  • 7,133
  • 7
  • 38
  • 64
SarangaR
  • 744
  • 6
  • 18
0

In your autocomplete method in jquery, make a call with Ajax to get data from your database through the controller. If you are not familiar with Ajax, you have to invest some time in learning. Here is a rough example to guide you how the things will work.

searchUrl ='http://www.yourwebsite.com/travelflyers/search'
 $( "#autocomplete" ).autocomplete({
    $.ajax({url: searchUrl, success: function(flyers){
    source: flyers;
 });

Another example here

It is not the exact example, but I hope you'll get the idea.

Community
  • 1
  • 1
Qammar Feroz
  • 708
  • 8
  • 21
0

If you use typehead why you add a form and submit button you have to put only text box with id. typehead is working as ajax when you type some word ajax request to your controller and get the result

<div id="the-basics">
  <input class="typeahead" type="text" placeholder="Title">
</div>

    <script>
    $('#the-basics .typeahead').typeahead({
      hint: true,
      highlight: true,
      minLength: 1
    },
    {
      name: 'states',
      source: 'travelflyers/search'
    });

    </script>

and make sure your controller return JSON data don't return view it should be json data use var_dump in your controller and check from browser development tool it is json or not

SarangaR
  • 744
  • 6
  • 18
  • Here I updated my view and controller, still doesnt show autocomplete – David Feb 07 '16 at 02:43
  • please check your typehead.js link is correct (check from inspect element and click on the link) then check in network tab (in chrome console) when you type letters ajax request is working or not, then in your controller put dd(input:all()); check these things first – SarangaR Feb 07 '16 at 02:47
0

change your routes to get method and pass query variable

Route::get('travelflyers/search/{query}',[
    'uses' => '\App\Http\Controllers\TravelFlyersController@search',
    'as'   => 'travelflyers.search',
]);

in your view

<script src="https://twitter.github.io/typeahead.js/js/jquery-1.10.2.min.js"></script>
<script src="https://twitter.github.io/typeahead.js/releases/latest/typeahead.bundle.js"></script>

<div id="remote">
  <input class="typeahead" type="text" placeholder="Search Travel Flyers Here">
</div>
    <script>
    var bestPictures = new Bloodhound({
      datumTokenizer: Bloodhound.tokenizers.obj.whitespace('title'),
      queryTokenizer: Bloodhound.tokenizers.whitespace,
      remote: {
        url: '/travelflyers/search/%QUERY',
        wildcard: '%QUERY'
      }
    });

    $('#remote .typeahead').typeahead(null, {
      name: 'best-pictures',
      display: 'title',
      source: bestPictures
    });

    </script>

in your controller

public function search($query) {        
    $flyers = Flyer::select('title')->where('title', 'LIKE', '%' .$query. '%')->get();
    return $flyers;
}

its not styled yet because i just copy and paste from official documentation, i already try that code and it works..

  • the typeahead is working but when I click enter it doesnt show or do anything – David Feb 07 '16 at 03:44
  • what do u want to do? redirect to open that flyer? or show it in current view? if you want show in current view can u show me your full blade? – Jimmy Wijaya Feb 07 '16 at 04:25
  • well what I currently have is when user searches for a flyer, it takes them to a search.php with a list of all flyers with that search key word – David Feb 07 '16 at 18:05