2016-07-26 2 views
1

Я пытаюсь построить поиск на своем сайте (используя laravel 5.2). Мне нужно искать сразу несколько таблиц. В основном мне нужно отображать информацию профиля, фильтруя категорию, работу, отдел и город!фильтрация с поиском eloquent

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

Благодарим за отзыв; Я предпочитаю использовать красноречивый. с построителем запросов Я столкнулся с этой ошибкой: Неопределенная переменная: request – nabil

+0

Вы должны создать запрос Request $ request в своем контроллере. 'Публичная функция save (Запрос $ request) {}' –

0

спасибо Akram, он работает, также добавили фильтр по городам, как показано ниже:

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')); 
    } 
Смежные вопросы