在 Laravel 应用程序(博客/帖子)上开发搜索功能。有多种类型的帖子(每个帖子在数据库中都有一个单独的表),例如商业帖子、社交生活帖子等。
下面是 SearchController 上的搜索功能
class SearchController extends Controller
{
public function search(Request $request, $query = null)
{
if($query == null)
return redirect()->route('home');
$search = Business::where([['title','like','%'.$query.'%'],['status','=',1]])
->orWhere([['description','like','%'.$query.'%'],['status','=',1]])
->paginate(10);
return view('front.search',[
'results' => $search,
'query' => $query
]);
}
}
所以基本上我的问题是如何添加其他类型的 Post 表?
我的主要动机是,当有人搜索任何内容时,应该从所有类型的帖子表(商业、自然、生活等..)中获取结果。
您必须在两个表中维护公共 ID
注意:加入是首选方法
$querys = DB::table('Business')->where([['Business.title','like','%'.$query.'%'],['Business.status','=',1]])
->orWhere([['Business.description','like','%'.$query.'%'],['Business.status','=',1]]);
$querys->join('socialtable','socialtable.userid','=','Business.userid');
// Just join the social table
$querys->where('socialtable.title', 'like','%'.$query.'%');
$result = $querys->paginate(10);
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句