I'm relatively green with Laravel and PHP. I'm learning Laravel by way of the documentation and SO/Google. I've clicked on every typeahead/laravel question I could find here, and I just can't seem to find an example that quite fits. This question comes very close. I actually don't see any glaring differences. But then again, that OP was having trouble with remote as well.
I think I'm close, as my various pieces seem to be working individually. Let me explain...
I have a form...
<div class="input-group form-group" id="ingredients">
{{ Form::label('ingredients', 'Ingredients') }}
{{ Form::text('ingredients[]', null, array(
'class' => 'form-control typeahead',
'data-provide' => 'typeahead',
'data-items' => '10',
'id' => 'ingredients')
) }}
</div>
... with the script...
<script>
$('.typeahead').typeahead([
{
name: 'ingredients',
remote: '/recipe/create/%QUERY'
}
]);
</script>
remote:
points to this route
Route::get('recipe/create/{query}', 'SearchController@searchIngredients');
Which points to this controller method...
public function searchIngredients($query)
{
$data = array();
$results = Ingredient::select ('name')->where('name', 'LIKE', '%' . $query . '%')->get();
foreach ($results as $result) :
$data[] = $result->name;
endforeach;
var_dump($data);
return Response::json($data);
}
When I enter some text in the form, I do see the browser request:
If I navigate directly to the remote URL, I do see my var_dump
as I would expect:
E.g. /recipe/create/s
shows me
array(4) {
[0] "Sugar"
[1] "Cheese"
[2] "Salt"
[3] "rasins"
}
I'm clearly missing something, and I have a suspicion that it's simple, though I could be wrong there too. When I begin typing in the form field, there is no dropdown.
I'm thinking my controller method is never being called when typing, or I would be seeing the var_dump
with every browser request, right?
If that's the case, am I broken at the remote:
bit of the script?
EDIT
This will work and tested:
Try taking
var_dump
out of the controller - typeahead is expecting valid JSON as a response.By the way, you could have done