May 9, 2017
0
Level 1
Ajax search form giving Object of class Symfony\Component
Quiz app, Trying to make a search form with ajax, but getting this when I click submit, using mac,laravel 5.2.29
ErrorException in QuizController.php line 32: Object of class Symfony\Component\HttpFoundation\ParameterBag could not be converted to string
This is my form
<form class="navbar-form navbar-left" id="formSearch" method="post" action="{{route('quiz.searchMain')}}">
{{ csrf_field() }}
<div class="form-group">
<input type="text" id="query" class="form-control" placeholder="Search">
</div>
<button id="searchSubmit" type="submit" class="btn btn-default">Submit</button>
</form>
and my ajax jquery code
$("#searchSubmit").click(function(){
$.ajax({
type:'POST',
url:'quiz/searchMain',
data:{
'_token':$('input[name=_token]').val(),
'query':$('#query').val()
},
success:function(data){
console.log("we succeded" + data);
}
});
});
right now testing to see if it returns data properly with console.log, but aiming to make it autocomplete eventually
controller that gives the response data QuizController.php
public function searchMain(Request $request){
$query = $request->query;
$results = Quiz::where('title','like',"$query%")->get();
if (Request::wantsJson()) return $results;
}
I guess there is a problem with $results since its on line 32, how would I fix this and any tips to improve the implementation ajax search for this would be good.
Please or to participate in this conversation.