I can't figure out what's going on here. I'm trying to make a pretty simple ajax post to do an autocomplete in laravel.
I have an input box and a spot for the results:
<div class="form-group">
<input type="text" name="tag_name" id="tag_name" class="form-control input-lg" placeholder="Enter Country Name" />
<div id="tagList">
</div>
</div>
and my JS
$('#tag_name').keyup(function(){
var query = $(this).val();
if(query != '')
{
//var _token = $('input[name="_token"]').val();
$.ajax({
url:"{{ route('campaigns.search') }}",
method:"POST",
data:{query:query, _token: '{{ csrf_token() }}'},
success:function(data){
$('#tagList').fadeIn();
$('#tagList').html(data);
}
});
}
});
$(document).on('click', 'li', function(){
$('#tag_name').val($(this).text());
$('#tagList').fadeOut();
});
});
The route points to my controller function:
public function searchTags(Request $request)
{
if($request->get('query'))
{
$query = "%" . $request->get('query') . "%";
$data = CampaignTags::where('TAG_DATA', 'LIKE', $query)->get();
$output = '<ul>';
foreach ($data as $row) {
$output .= '<li><a href="#">' .$row->TAG_DATA. '</a></li>';
}
}
return json_encode($data);
}
When I inspect as I type, I get 200 codes on the search but I'm not getting actual results to show from the database, the response seems to be null
I did this using typeahead. and answered it in another thread. before
heres the link. Auto Complete Laravel