1

I am using typeahead in my current project along with simple query search. The simple query search is working perfectly but, the typeahead is partially working i.e SearchController@autocomplete is responding with JSON and in app.blade.php i have typeahead jquery script that script is not rendering/converting json into HTML elements.

Even the console is not giving any errors. I am not able to understand the behind the scene of the problem. I am providing all by source code.

app.blade.php

<script src="/js/jquery.min.js"></script>
    <script src="/js/bootstrap.min.js"></script>
    <script src="/js/typeahead.js"></script>
    <script type="text/javascript">

            var path = "{{ route('autocomplete') }}";
            $('input.typeahead').typeahead({
                source:  function (query, process) {
                    return $.get(path, { query: query }, function (data) {
                        return process(data);
                    });
                }
            });

    </script>
    @yield('scripts')

</body>
</html>

navbar.blade.php

<form class="navbar-form navbar-left" role="search" action="/search" autocomplete="off">
                <div class="form-group">
                    <input type="text" class="typeahead form-control" id="query" placeholder="Search Staff By Name, Designation, Department" name="query" value="{{Request::input('query')}}">

                </div>
                <button type="submit" class="btn btn-default">Submit</button>
            </form>

routes/web.php

/** * Search Controller Starts */

Route::get('/search', 'SearchController@getResults');
Route::get('/autocomplete',[
    'uses'=>'SearchController@autocomplete',
    'as'=>'autocomplete'
]);

/**
 * Search Controller Ends
 */

SearchController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Auth;
use App\User;
use DB;

class SearchController extends Controller
{
    public function getResults(Request $request)
    {   $query = $request->input('query');
        if(!$query){
         return redirect('/home');
        }

        $users = User::where(DB::raw("CONCAT(first_name,'',last_name)"), '
                        LIKE', "%{$query}%")
                        ->orwhere('name', 'LIKE', "%{$query}%")
                        ->orwhere('department', 'LIKE', "%{$query}%")
                        ->orwhere('designation', 'LIKE', "%{$query}%")
                        ->orwhere('email', 'LIKE', "%{$query}%")
                        ->orwhere('phone', 'LIKE', "%{$query}%")
                        ->get();

        return view('search')->with('users', $users);
    }

    public function autocomplete(Request $request){
        $data = User::where("name","LIKE","%{$request->input('query')}%")->get();
        return response()->json($data);
    }

    }

Some Screen Shots

enter image description here

enter image description here http://localhost:8000/autocomplete is ok

G Naga Subrahmanyam
  • 308
  • 1
  • 5
  • 19
  • `$('input.typeahead').typeahead({` not sure `input.typeahead` or simply `.typeahead` or `input .typeahead` [topic:jquery selector](https://api.jquery.com/category/selectors/) -- looking at the console's network tab your json should be passed successfully to the page, that being said, the remaining culprit ought to be in the typeahead script itself. – Bagus Tesa Dec 07 '16 at 03:40
  • No JSON is not passing in to my console's network tab. Also i have used #query selector in jquery. – G Naga Subrahmanyam Dec 07 '16 at 03:47
  • *No JSON is not passing in to my console's network tab.* even when you type something in the text box..? so, do the script even pass a request to your `route('autocomplete')`? well, just realized [bootstrap and type ahead split](http://stackoverflow.com/questions/9232748/twitter-bootstrap-typeahead-ajax-example) not to mention most [examples here](https://twitter.github.io/typeahead.js/examples/) uses either `Bloodhound` or custom string matcher.. perhaps posting your bootstrap version and typeahead version can help someone to help you. – Bagus Tesa Dec 07 '16 at 04:09
  • @BagusTesa my assets are working awesome. – G Naga Subrahmanyam Dec 07 '16 at 04:15

0 Answers0