I have a problem with ajax searching in my project.
This is my view:
<div class="col-md-12">
{!! Form::open(['class' => 'form-horinzontal']) !!}
{!! Form::text('search', null, array('id' => 'search-input','required','class'=>'form-control','placeholder'=>'Product zoeken, begin met typen...','onkeyup' => 'search_data(this.value, "result")', 'autofocus')) !!}
{!! Form::close() !!}
<br>
<script>
function search_data(search_value) {
console.log(search_value);
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': '{{ csrf_token() }}'
}
});
$.ajax({
type : 'get',
url : '{{ URL::to('/webshop/products/searchProduct') }}',
data : {'search':search_value},
success:function(data) {
console.log(data);
}
});
}
</script>
</div>
the Controller:
public function searchProduct(Request $request){
if($request->ajax()) {
$output = "";
$products = Product::where('beschrijving', 'LIKE','%'. $request['search'] .'%')->get();
if(count($products)) {
foreach($products as $product){
$output .= '<tr>' .
'<td>' . $product->beschrijving . '</td>' .
'<td>' . $product->artikelcode . '</td>' .
'<td>' . $product->prijs . '</td>' .
'</tr>';
}
return response($output);
} else {
return response('TESTING!');
}
}
}
and the route:
Route::group(['prefix' => '/webshop/products'], function() {
Route::get('/searchProduct', 'ProductsController@searchProduct');
});
i get a completely blank response, which is weird.
I also tried returning strings instead of the $ouput variable, the controller returns nothing at all.
Im getting no errors either, and i'm out of options. The response im getting:
My route was wrong:
Had to remove the get from the prefix group, then it worked.