如何为我正在尝试创建的搜索引擎使用whereIn子句。我有两个输入,一个用户输入他们正在寻找的内容,另一个用于城市,州或邮政编码。当我搜索我正在寻找的东西时,它会显示结果的城市,当我寻找它显示结果的状态,当我查找邮政编码时,它会显示结果,但是当我输入城市和州时,它不会显示结果。有人可以帮我这个吗?这就像我第四次在Stack溢出时发布的那样,似乎仍然没有像我预期的那样运行。这是我的代码
$userinput=$request->input('mywhat');
$userinputt=$request->input('mywhere');
$str = trim(preg_replace('/\s+/',' ', $userinput));
$strr = trim(preg_replace('/\s+/',' ', $userinputt));
$this->validate($request,array(
$strr=>'nullable',
));
$pro=Profile::Where('what','Like','%'.$str.'%')->Where(function ($query) use($strr, $userinputt) {
$query->Where('city', 'LIKE', '%'.$strr.'%');
})->orWhere('what','Like','%'.$str.'%')->Where(function ($query) use($strr, $userinputt) {
$query->Where('state', 'LIKE', '%'.$strr.'%');
})->orWhere('what','Like','%'.$str.'%')->Where(function ($query) use($strr, $userinputt) {
$query->Where('zipcode', 'LIKE', '%'.$strr.'%');
//}) - > orWhere('what','like','%'。$ str。'%') - > Where(function($ query)use($ strr,$ userinputt){ // $ query-> Where('city','LIKE','%'。$ strr。'%');
})->orWhere('what','Like','%'.$str.'%')
->Where('city', 'LIKE', '%'.$strr.'%')
->Where( function ($query) use ($strr,$userinputt) {
$query->Where('state', 'LIKE', '%'.$strr.'%');
$query->orWhere('zipcode', 'LIKE', '%'.$strr.'%');
})->inRandomOrder()->paginate(5);
return view('layouts.reveal')->with('pro',$pro);
答案 0 :(得分:0)
了解这会带给您什么(当然,您需要更改模型和变量以适合您的代码):
$query = Model::where('search', 'like', '%' . $search . '%')
->when($city, function ($query) use ($city) {
return $query->whereCity($city);
})
->when($state, function ($query) use ($state) {
return $query->whereState($state);
})
->when($zip, function ($query) use ($zip) {
return $query->whereZip($zip);
})
->get();