Displaying Results From Autocomplete Search Laravel
I applied Typeahead autocomplete search to my 'master.blade' and now I have search field available inside my navigation bar that is included everywhere inside my site. My autocompl
Solution 1:
To convert a model to JSON, you should use the toJson()
method. Like toArray
, the toJson
method is recursive, so all attributes and relations will be converted to JSON:
publicfunctionautocomplete(Request $request)
{
if( !empty(request('query'))){
$data = Product::select("title")
->where("title","LIKE","%{$request->input('query')}%")
->get();
$dataJson =$data->toJson();
return view('master', compact('dataJson'));
}elsereturn view ('master');
}
If you really want to use json encoded data in a view, use json_decode()
in foreach:
@if($dataJson)
@foreach(json_decode($dataJson, true) as $value)
{{ $value['record1']['record 2'] }}
@endforeach
@endif
Post a Comment for "Displaying Results From Autocomplete Search Laravel"