2015-10-06 167 views
1

我嘗試爲用戶創建搜索引擎。搜索將包含多個字段,以便用戶可以選擇他想要的任何內容並獲得結果。合併多個查詢(Laravel 5)

routes.php文件:

Route::get('search/{tag?}/{town?}/{education?}/{contract?}', '[email protected]'); 

DisplayJobs.php控制器

public function getSearch($tag = null, $town = null, $education = null, $contract = null) 
{ 
    //get already database values to send them to the form 
    $tags = \App\Tag::lists('name', 'id'); 
    $contract = \App\Contract::lists('name', 'id'); 
    $towns = \App\Town::lists('name', 'id'); 
    $education = \App\Education::lists('name', 'id'); 

    $tagQueryBuilder = Tag::query(); 
    $townQueryBuilder = Town::query(); 
    $educationQueryBuilder = Education::query(); 
    $contractQueryBuilder = Contract::query(); 

    if(Input::has('tag')) 
    { 
     $tagQueryBuilder->TagOfUser(Input::get('tag')); 
    } 
    if(Input::has('town')) 
    { 
     $townQueryBuilder->TownOfUser(Input::get('town')); 
    } 
    if(Input::has('education')) 
    { 
     $educationQueryBuilder->EducationOfUser(Input::get('education')); 
    } 
    if(Input::has('contact')) 
    { 
     $contractQueryBuilder->ContactOfUser(Input::get('contact')); 
    } 


    return view('main.search_jobs', compact('tags', 'towns', 'contract', 'education')); 

} 

如果我嘗試用它完美的每個單獨的查詢,但我想綜合作用的結果數據從所有查詢或一次查詢所有數據的方式。

在每一個模型,我有我的查詢範圍是這樣(Tag.php)型號

public function jobs() 
{ 
    return $this->belongsToMany('App\Job'); 
} 

public function scopeTagOfUser($query, $tag) 
{ 
    return $query->where('id', '=', $tag)->with('jobs'); 
} 
+0

你的問題不明確。你想要所有查詢的組合結果數據,還是想要一種方法一次查詢所有數據?此外,'\ App \ Something :: lists('name','id');'工作,這讓我想知道,你甚至沒有運行代碼? – JSelser

+0

謝謝你的回覆。所有代碼\ App \ Something :: lists('name','id');完美的作品,我嘗試過。我只想結合所有查詢的結果數據或一次查詢所有數據的方法。 –

+0

此外,它適用於每個單一的查詢。 –

回答

1

了很多小時後,我找到了解決辦法。我會在下面發佈,所以如果任何人有同樣的問題可以看到一個解決方案。

首先我必須刪除所有的車型範圍查詢,並完成所有的控制器像波紋管的工作:

public function getSearch($tag = null, $town = null, $education = null, $contract = null) 
{ 
    //get already database values to send them to the form 
    $tags = \App\Tag::lists('name', 'id'); 
    $towns = \App\Town::lists('name', 'id'); 
    $contract = \App\Contract::lists('name', 'id'); 
    $education = \App\Education::lists('name', 'id'); 

    //get inputs from users 
    $getTagFromUser = Input::get('tag'); 
    $getTownFromUser = Input::get('town'); 
    $getContractFromUser = Input::get('contract'); 
    $getEducationFromUser = Input::get('education'); 

    $tagQuery = DB::table('jobs') 
     ->join('job_tag', 'jobs.id', '=', 'job_tag.job_id') 
     ->join('tags', 'job_tag.tag_id', '=', 'tags.id') 
     ->where('tags.id', '=', $getTagFromUser); 

    $townQuery = DB::table('jobs') 
     ->join('job_town', 'jobs.id', '=', 'job_town.job_id') 
     ->join('towns', 'job_town.town_id', '=', 'towns.id') 
     ->where('towns.id', '=', $getTownFromUser); 

    $contractQuery = DB::table('jobs') 
     ->join('job_contract', 'jobs.id', '=', 'job_contract.job_id') 
     ->join('contracts', 'job_contract.contract_id', '=', 'contracts.id') 
     ->where('contracts.id', '=', $getContractFromUser); 

    $educationQuery = DB::table('jobs') 
     ->join('job_education', 'jobs.id', '=', 'job_education.job_id') 
     ->join('education', 'job_education.education_id', '=', 'education.id') 
     ->where('education.id', '=', $getEducationFromUser); 


    $finalQuery = $tagQuery->union($townQuery)->union($contractQuery)->union($educationQuery)->get(); 


    return view('main.search_jobs', compact('tags', 'towns', 'contract', 'education', 'finalQuery')); 

}