Laravel 5.3路由混淆搜索

时间:2017-01-21 10:03:50

标签: php laravel api search laravel-5.3

我正在使用我的API中的搜索功能,我对路径应该如何

感到困惑 来自移动最终用户的

搜索namephone_numberregistration_id我应仅提供这些字段作为回报

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"
    }
  ]
}

以上回复将在移动应用中显示为列表视图,选择特定列表(ID)可能是81用户发布id 基于id我应该返回工人的所有细节

我的方法和路线是

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
        ]);     
    }

我发送特定身份证的所有细节的其他方法

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
    ]);     
}

期待急需的帮助

谢谢

0 个答案:

没有答案