I’m making a searchbar for my website, I wanna filter users for username.
My function in controller look like this:
function search_users(Request $request) {
if ($request->search) {
$searchUsers = User::where('username', 'LIKE', '%'. $request->search. '%')->get();
$data = [
"users" => $searchUsers
];
//return response()->json($searchUsers);
return view('dashboard.home', $data);
}else {
return response()->json("no_results");
// return view('dashboard.home');
}
}
<form action="{{route('search')}}" method="get">
<input type="search" name="username" id="" placeholder="Cerca" class="searchbar">
<button type="submit">Search</button>
</form>
If I try to make a search I receive no_results response also if I have this username stored into db, probably I wrong to set something inside my blade file
2
Answers
Inside your form you named the input as
inside your controller you have to use the same name
Replace
$request->search
by$request->username
, because the name of your input isusername
and you used the type of the input