I'm working on search functionality in my API, i'm confused about how the route should be
from the mobile end user searches for name
or phone_number
or registration_id
i should give only those fields in return
http://localhost:8000/api/v1/search/Walter //name or
http://localhost:8000/api/v1/search/9665885563 //phone or
http://localhost:8000/api/v1/search/REFT254525 //regisration_id
//considering user types name Walter the response is below, which is fine
{
"data": [
{
"id": 1,
"first_name": "Walter",
"last_name": "White",
"phone_number": "9665885542",
"registration_id": "REFT254525"
},
{
"id": 8,
"first_name": "Mitty",
"last_name": "Walter",
"phone_number": "8826835542",
"registration_id": "REFT254528"
}
]
}
the above response will be shown as list view in mobile app, on selecting particular list(id) it may be 8
or1
the user post the id
based on the id
i should return all the details of the worker
my method and the route is
Route::get('search/{list}', 'DriversController@getSearchResults'); //route
public function getSearchResults(Request $request, $list) {
$search_drivers = Driver::where('id', 'like', $list)
->orWhere('first_name', 'like', $list)
->orWhere('last_name', 'like', $list)
->orWhere('phone_number', 'like', $list)
->orWhere('registration_id', 'like', $list)
->select('id','first_name','last_name','phone_number','registration_id')
->get();
return Response::json([
'data' => $search_drivers
]);
}
my other method for sending all the details of particular id
public function getSearchData(Request $request, $id) {
$data = $request->get('data');
$search_drivers = Driver::where('id', $id)
->get();
if(! $search_drivers) {
return $this->respondNotFound();
}
return Response::json([
'data' => $search_drivers
]);
}
looking forward for much needed help
thank you