我有一个控制器,从搜索表单中获取四个输入。
SearchController.php 代码
public function results(Request $request) {
$text = $request -> text;
$pet = $request -> pet;
$category = $request -> category;
$city = $request -> city;
$searchArray = [];
if(empty($text) && empty($pet) && empty($category) && empty($city)) {
Session::flash('danger', "You didn't select any search any search.");
return redirect() -> back();
}
//SEARCH CODE HERE
}
我想要做什么
我正在尝试在我的数据库中搜索4列。
问题是
我还需要在一个查询中搜索这4个列。
这意味着我需要检查$text
变量和$pet
变量是否为空,然后执行以下查询:
if(!empty($text) && !empty($pet))
$result = Post::where('text', 'like', '%'.$text.'%') -> where('text', $pet) -> get();
这种方法可以正常工作,但是我需要多个if语句来检查所有可能性。
是否有更快速和更优化的解决方案?