2016-07-26 6 views
1

私は自分のウェブサイトで検索を構築しようとしています(laravel 5.2を使用しています)。私は一度に複数のテーブルを検索する必要があります。基本的には、カテゴリ、職種、部署、市区町村をフィルタリングしてプロファイルの情報を表示する必要があります。検索の雄弁でフィルタリングする

profiles : id, name, ......, job_id, location_id...... 
location : id, name, city_id 
job : id, name, categorie_id 
categorie : id, name 
city : id, name 


in below code : 
$profils = \App\Profils::whereHas('jobs', function($query){ 
       $nom_catego = \Input::has('lcategorie') ? \Input::get('lcategorie') : null; 
       $nom_job = \Input::has('ljob') ? \Input::get('ljob') : null; 
       $nom_location = \Input::has('ldept') ? \Input::get('llocation') : null; 
     if(!isset($nom_catego) && !isset($nom_job)){ 
        $query->where('categorie_id', '=' , $nom_catego) 
          ->where('id', '=', $nom_job); 
     } 
     if(!isset($nom_catego) && isset($nom_job)){ 
      $query->where('categorie_id', '=' , $nom_catego); 
     } 
     if(!isset($nom_job) && !isset($nom_location) && isset($nom_catego)){ 
        $query->where('city_id', '=' , $nom_location) 
           ->where('id', '=' , $nom_catego); 
     } 
     if(isset($nom_job) && !isset($nom_location) && isset($nom_catego)){ 
      $query->where('city_id', '=' , $nom_location); 
     } 
    })->paginate(10); 

NB:このコードでは、カテゴリとジョブでプロファイルを取得できますが、都市と場所別にプロファイルを取得することはできません。 ご協力いただきありがとうございます。

答えて

0

あなたは、単にクエリビルダまたは説得力でそれを行う、クエリビルダで利用できるものは何でも機能することは、あまりにも、私は簡単にするために、クエリビルダのアプローチで私の答えを与えている 雄弁なクエリのために利用可能であることに注意し、

$results = DB::table('profiles') 
     ->select(['profiles.*']); 

    if($request->has('lcategorie')){ 
      $results->join('job','profiles.job_id','=','job.id');  
      $results->join('categorie','job.categorie_id','=','categorie.id'); 
      $results->where('categorie.id','=',$request->input('lcategorie')); 

    } 

    if($request->has('ljob')){ 
     if(!$request->has('lcategorie')){ //avoid repeat the join to same table multiple time 
      $results->join('job','profiles.job_id','=','job.id');  
     } 

     $results->where('job.id','=',$request->input('ljob')); 
    } 

    if($request->has('ldept')){ 
     $results->join('location','profiles.location_id','=','location.id'); 
     $results->join('city','location.city_id','=','city.id'); 
     $results->where('location.id','=',$request->input('llocation')); 
    } 

    $results = $results->get(); 
+0

はREPONSEいただきありがとうございます。 私は雄弁を使うのが好きです。クエリビルダーで このエラーが発生しました:未定義の変数:request – nabil

+0

あなたのコントローラでRequest $ requestをインスタンス化する必要があります。 'パブリック関数save(Request $ request){}' –

0
することができます

はあなたにアクラムに感謝、それは動作しますが、また次のように都市によってフィルタリングを追加しました:

if($request->has('ldept')){ 
      $profiles->join('location','profiles.location_id','=','location.id');  
      $profiles->join('citys','location.city_id','=','citys.id'); 
      $profiles->where('citys.id','=',$request->input('ldept')); 

    } 

if($request->has('llocation')){ 
     if(!$request->has('ldept')){ 
      $profiles->join('location','profiles.location_id','=','location.id');  
     } 

     $profiles->where('location.id','=',$request->input('llocation')); 
    }