首页 > 解决方案 > 加入 Mongodb 并使用 laravel 在连接表中搜索

问题描述

我想使用 mongodb 数据库连接 laravel 中的表,并在两个表中搜索参数。我用过 has 但它不起作用。我想在具有类似条件的连接表中搜索参数。

我试过有条件。

$CustomerData = Customer::orderBy('_id', 'desc');
$CustomerData = $CustomerData
->where('first_name', 'like', '%' . $search . '%')
->orWhere('middle_name', 'like', '%' . $search . '%')
->with('addressData')  
    ->whereHas('addressData',       
        function($query) use ($search)  
        {          
           $query->orWhere('country', 'like', '%' . $search . '%');       
           $query->orWhere('state', 'like', '%' . $search . '%');          
         });

标签: phpmongodblaravel

解决方案


@Vishal 检查此示例代码以在不同表之间进行搜索:

代码在这里:

  public function SimpleSearch($term)
  {
    $match = "MATCH(authors.authors) AGAINST(? IN BOOLEAN MODE)";
    $match1 = "MATCH(keywords.keywords) AGAINST(? IN BOOLEAN MODE)";
    $match2 = "MATCH(research_papers.title) AGAINST(? IN BOOLEAN MODE)";
    $match3 = "MATCH(research_papers.abstract) AGAINST(? IN BOOLEAN MODE)";
    $researchpapers = DB::table('keywords')
    ->join('research_papers','keywords.researchpaper_id','=','research_papers.id')
    ->join('authors','authors.researchpaper_id','=','research_papers.id')
    ->leftjoin('favorite_models','favorite_models.researchpaper_id','=','research_papers.id')
    ->whereRaw($match1,array($term))
    ->orwhereRaw($match2,array($term))
    ->orwhereRaw($match3,array($term))
    ->orwhereRaw($match,array($term))
    ->orderbyRaw($match2."DESC",array($term))
    ->orderby('dates','desc')
    ->select('research_papers.id','research_papers.title','research_papers.dates','research_papers.abstract','research_papers.paperlinks','authors.authors','keywords.keywords','research_papers.doi','favorite_models.researchpaper_id','research_papers.websites')
    ->paginate(10);

    return $researchpapers;
  }

我希望它会有所帮助。它对我有用。


推荐阅读