This is my search form, all results displayed are users on my site using JQuery autocomplete. When I select the searched user's name, I simple want the form to go to the go to the profile page of the user on Submit or when I click on the selected user's name and bring up their profile. Much like Facebook.
<form action="" class="navbar-form navbar-right" role="search">
<div class="input-group">
<input type="text" name="term" class="form-control inputsearch" placeholder="Search..." />
<span class="input-group-btn">
<button class="btn btn-default searchbar" type="submit">
<i class="fa fa-search searchnow"></i>
</button>
</span>
</div>
</form>
This is the JQuery autocomplete function
$(function() {
$("input[name=term]").autocomplete({
source: "autocomplete",
minLength: 3,
select: function(event, ui) {
$(this).val(ui.item.value);
}
});
});
These are my routes for the autocomplete and getting the user's profile
Route::get('/dashboard/{name}', [
'uses' => 'UserController@getProfile',
'as' => 'profile.index',
]);
Route::get('/autocomplete',[
'uses'=>'UserController@autocomplete',
'as'=>'autocomplete'
]);
And finally, this is my controller:
public function autocomplete(Request $request)
{
if ($request->ajax())
{
return User::select(['id', 'name as value'])->where(function($query) use ($request) {
if ( ($term = $request->get("term")) )
{
$keywords = '%' . $term . '%';
$query->orWhere("name", 'LIKE', $keywords);
$query->orWhere("email", 'LIKE', $keywords);
}
})
->orderBy('name', 'asc')
->take(5)
->get();
}
}
public function getProfile($name)
{
$user = User::where('name', $name)->first();
if(!$user){
abort(404);
}
$posts = Post::where("user_id", "=", $user->id)->latest()->paginate(3);
$photos = Photo::paginate(6);
return view('profile.index',compact('user','posts', 'photos'));
}
What can I modify to make this work?
try something like this, the code doesnt work but it gives you an idea
$(function() {
$("input[name=term]").autocomplete({
source: "autocomplete",
minLength: 3,
select: function(event, ui) {
var userid = ui.item.value; // just try to get user id here
window.location.href = "yourapp/user/"+userid ;
}
});
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.