i'm working on search functionality, i'm querying based on the user input but i'm not getting the result as expected
//if user searches walter getting proper response
http://localhost:8000/api/v1/search/walter
{
"data": [
{
"id": 1,
"first_name": "Walter",
"last_name": "White",
"phone_number": "9665885542",
"registration_id": "12345b67892"
},
{
"id": 8,
"first_name": "Mitty",
"last_name": "Walter",
"phone_number": "8826835542",
"registration_id": "dffdfg54ty45y"
}
]
}
// but if user searches walt the response is
http://localhost:8000/api/v1/search/walt
{
"data": []
}
my method
//search Drivers public function getSearchResults($search_input) {
$search_drivers = Driver::where('id', 'like', $search_input)
->orWhere('first_name', 'like', $search_input)
->orWhere('last_name', 'like', $search_input)
->orWhere('phone_number', 'like', $search_input)
->orWhere('registration_id', 'like', $search_input)
->select('id','first_name','last_name','phone_number','registration_id')
->get();
return Response::json([
'data' => $search_drivers
]);
}
is there any better way to get results before entering full data
thank you
The correct syntax for like
is '%'.$search_input.'%'
:
$search_drivers = Driver::where('id', 'like', '%'.$search_input.'%')
->orWhere('first_name', 'like', '%'.$search_input.'%')
->orWhere('last_name', 'like', '%'.$search_input.'%')
->orWhere('phone_number', 'like', '%'.$search_input.'%')
->orWhere('registration_id', 'like', '%'.$search_input.'%')
->select('id','first_name','last_name','phone_number','registration_id')
->get();