I read about working it out through an ajax request, but somehow it doesn't work, yet.
This is the current setup
HomeController.php
public function search(Request $request){
if($request->ajax()){
$output="";
$users=DB::table('users')->where('first_name','LIKE','%'.$request->search."%")->get();
if($users){
foreach ($users as $key => $user) {
$output.='<tr>'.
'<td>'.$user->id.'</td>'.
'<td>'.$user->first_name.'</td>'.
'</tr>';
}
return Response($output);
}
}
}
web.php
Route::get('/searchusers','HomeController@search');
View:
<div class="form-group">
<input type="text" class="form-controller" id="search" name="search"></input>
</div>
<table class="table table-bordered table-hover">
<thead>
<tr>
<th>ID</th>
<th>First Name</th>
</tr>
</thead>
</table>
<script type="text/javascript">
$('#search').on('keyup',function(){
$value=$(this).val();
$.ajax({
type : 'get',
url : '{{URL::to('searchusers')}}',
data:{'search':$value},
success:function(data){
$('tbody').html(data);
}
});
})
$.ajaxSetup({ headers: { 'csrftoken' : '{{ csrf_token() }}' } });
</script>
Are there any obvious mistakes?