我正在为front end and mobile apps
实施API,现在我正在search function
工作,用户可以在其中输入column name
或任何data
我应该能够以JSON格式提供他们请求的数据
所以我做了这个
在我的控制器中
public function getSearchResults(Request $request) {
$data = $request->get('data');
$search_drivers = Driver::where('agent_id', 'like', "%{$data}%")
->orWhere('registration_center', 'like', "%{$data}%")
->orWhere('registration_id', 'like', "%{$data}%")
->orWhere('sponsor_name', 'like', "%{$data}%")
->orWhere('event_name', 'like', "%{$data}%")
->orWhere('registration_id', 'like', "%{$data}%")
->orWhere('profile_photo', 'like', "%{$data}%")
->orWhere('first_name', 'like', "%{$data}%")
->orWhere('last_name', 'like', "%{$data}%")
->get();
return Response::json([
'data' => $search_drivers
]);
}
使用参数i搜索获取JSON响应
http://localhost:8000/api/v1/search?data=center
//Response
{
"data": [
{
"id": 1,
"agent_id": "201701",
"registration_center": "Center",
"registration_date": "12-01-2017",
"sponsor_name": "Sponser Name",
"event_name": "Event Name",
"registration_id": "45345343543353",
"profile_photo": "",
"first_name": "",
"last_name": "",
}
]
}
但我如何过滤像这样的json
http://localhost:8000/api/v1/search?data=center
//Response
{
"data": [
{
"registration_center": "center",
}
]
}
是否可以这样做,请您告诉我或给我一些示例如何query and return as json
谢谢
答案 0 :(得分:4)
将['registration_center']
添加到get()
:
->get(['registration_center']);
或者在select()
之前使用get()
:
->select('registration_center')
->get();