【问题标题】:%like% query not giving result as expected [duplicate]%like%查询未按预期给出结果[重复]
【发布时间】:2017-06-06 08:31:45
【问题描述】:

我正在研究搜索功能,我正在根据用户输入进行查询,但我没有得到预期的结果

//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": []
}

我的方法

//搜索驱动程序 公共函数 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
]);     

}

在输入完整数据之前有没有更好的方法来获得结果

谢谢

【问题讨论】:

    标签: php sql laravel search laravel-5.3


    【解决方案1】:

    like 的正确语法是 '%'.$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();
    

    【讨论】:

    • 我的错,非常感谢你真的很感激,我不能在 6 分钟内接受答案,stackoverflow 说
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多