在Laravel中使用%LIKE%时获取空数组

时间:2018-04-23 09:29:44

标签: php laravel laravel-5 vue.js axios

我从数据库中获取位置。我在控制器中有这样的代码:

public function getLocationsList(Request $request,$searchKey)
{
   $searchedLocations = Hotel::where('location', 'LIKE', "%$searchKey%")->get();
   return $searchedLocations;
}

当我在数据库中有记录时,我不明白为什么$searchedLocations为空。

我正在使用VueJs和axios发送请求。

enter image description here

请帮忙。

1 个答案:

答案 0 :(得分:0)

从评论(dd($searchKey))中,我可以看到您正在调用此网址

http://localhost:8000/getLocations/%7Ban%7D 

或(相同)

http://localhost:8000/getLocations/{an}

通过以这种方式调用URL,您实际上在SQL语句中包括大括号:

Select * from location like "%{an}%"

这就是你得到空结果的原因。

要解决您的问题,只需更改网址删除括号

http://localhost:8000/getLocations/an