ken4ward
ken4ward

Reputation: 2296

Typeahead not working with Laravel 5.1

I wonder why this is not pulling records from the database. I have been on this for days. All I want to do is use typeahead to select matching record from the db. When a user enters a character, the matching records should be displayed so that the user can select the relevant value. I hope a kind heart would help me resolve this.

THis is the controller:

public function SelectLocationPlaces(UserAdressRequest $userAdressRequest)
        {
             if($userAdressRequest->isMethod('post'))
             {
                if($userAdressRequest->has('query'))
                {
                    $query = $userAdressRequest->get('query');
                    $locationPlaces = LocationPlaces::where('name', 'like', "%{$query}%")->toArray();
                    return json_encode($locationPlaces);
                }
             }
        }

This is the blade form:

{!! Form::open(array('url' => 'created-products', 'method' => 'post'), array('id'=>'createproduct')) !!}
{!! Form::token() !!}

    <link href="{!! asset('bootstrap/css/bootstrap.css') !!}" media="all" rel="stylesheet" type="text/css" />
    <link href="{!! asset('css/style.css') !!}" media="all" rel="stylesheet" type="text/css" />
    <!--<link rel="stylesheet" type="text/css" href="style.css">-->
    <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>

<p>{!! Form::select('countries', array('-1' => 'Select a Country') + $listedCountries) !!} </p>
<div class='well'>
    <p>{!! Form::text('query', '', array('id'=>'typeahead', 'data-provider'=>'typeahead')) !!}</p>
</div>

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
    <script src="{!! asset('bootstrap/js/bootstrap.js') !!}" type="text/javascript" />

    <script>
        $(function(){
                $('#typeahead').typeahead({
                    source: function(query, process){
                        $.ajax({
                            url: '{{ url('json/redirects/') }}',
                            type: 'POST',
                            data: 'query=' +query,
                            dataType: 'JSON',
                            async: true,
                            success: function(data){
                                process(data);
                            }
                        });
                    }
                });
            });
    </script>

{!! Form::close() !!}

This is the route:

Route::post('json/redirects', array('before'=>'csrf', 'uses'=>'UserAddressController@SelectLocationPlaces'));

Upvotes: 1

Views: 266

Answers (1)

Marcin Nabiałek
Marcin Nabiałek

Reputation: 111829

You haven't written what's the problem - if it's exception or something else, but instead of:

$locationPlaces = LocationPlaces::where('name', 'like', "%{$query}%")->toArray();

you should rather use:

$locationPlaces = LocationPlaces::where('name', 'like', "%{$query}%")->get()->toArray();

Upvotes: 2

Related Questions