使用laravel,这是我的控制器文件中的一些代码:
public function postsearch(Request $request){
$searchItem=$request['searchItem'];
$searchLocation=$request['searchLocation'];
$criteria=$request['criteria'];
if($criteria == 'schools'){
$result=schools::whereRaw("MATCH(name,describtion) AGAINST('$searchItem') AND MATCH(location) AGAINST('$searchLocation')")->where('status','')->get();
if(count($result)>0){
$count=count($result);
return view('schools', array('result' => $result))->with(array('searchItem'=>$searchItem,'searchLocation'=>$searchLocation,'criteria'=>$criteria,'count'=>$count))->with('searchItem',$searchItem);
}
else{
return view('schools')->with(array('msg'=>'Oops, No result found','count'=>'0','searchItem'=>$searchItem,'searchLocation'=>$searchLocation,'criteria'=>$criteria));
}
}
这是我正在尝试编辑的代码:
$result=schools::whereRaw("MATCH(name,describtion) AGAINST('$searchItem') AND MATCH(location) AGAINST('$searchLocation')")->where('status','')->get();
如果我正在尝试搜索学校,但我只是输入学校名称而不是位置,我得到0结果。我必须把它放在学校所在的位置。但是我想这样做,以便网站在每个地方显示学校,如果该人不知道该位置。现在,我必须放入学校的位置,以便在结果中显示。
其次,如果学校名称是“佛罗里达州立大学(FSU)”,我必须输入“佛罗里达州立大学”才能让学院出现。 “佛罗里达”或“FSU”不起作用。有没有办法解决它,以便如果搜索查询匹配名称的任何部分,数据库中的项目会显示?
更新:
不得不支付一个人几块钱来提出一个有效的代码:
$result=schools::where('<put the variable you are searching for here>', 'LIKE', '%'.$searchItem.'%')
->where('location', 'LIKE', '%'.$searchLocation.'%')
->where('status','')->orderBy('name', 'asc')->get();
试试这个:
$result=schools::whereRaw("MATCH(name,describtion) AGAINST('$searchItem') OR MATCH(location) AGAINST('$searchLocation')")->where('status','')->get();
$result=schools::whereRaw("MATCH(name,describtion)
AGAINST('$searchItem') AND MATCH(location)
AGAINST('$searchLocation')")->where('status','')->get();
替换为此
$result = schools::where(function ($q) use ($searchItem, $searchLocation)
{
return $q->where('name', 'LIKE',
$searchItem. '%')
->orWhere('description', 'LIKE', '%' .
$searchItem. '%');
->orWhere('location', 'LIKE', '%' .
$searchLocation. '%');
});
$result->where('status','')->get();
你可以改变这个
$result=schools::whereRaw("MATCH(name,describtion)
AGAINST('$searchItem') AND MATCH(location)
AGAINST('$searchLocation')")->where('status','')->get();
至
$result=schools::where('name', 'LIKE', '%'.$searchItem.'%')
->orWhere('describtion', 'LIKE', '%'.$searchItem.'%')
->orWhere('location', 'LIKE', '%'.$searchLocation.'%')
->where('status','')->get();
我也注意到你写过'描述'。你确定它不是描述吗?还有为什么要检查空字符串的状态?