2015-11-06 82 views
2

我想使用过滤器搜索功能在我的“属性”表中选择一些注册表。Laravel:加入关闭内关闭。雄辩

在我的控制器中,我收到过滤器并使用高级进行处理,其中在使用过滤器“房间”时,需要查看其他表相关的注册表。为此,我尝试在where闭包内部进行连接,但连接根本无法工作,搜索完成时忽略了连接。

控制器:

 $filter_type= Input::has('filter_type') ? Input::get('filter_type') : NULL; 
    $filter_val= Input::has('filter_val') ? Input::get('filter_val') : NULL; 

    $state= NULL; 
    $sub_category= NULL; 
    $cat_operation= NULL; 
    $room= NULL; 

    if($filter_type == 'state'){ 
     $state     = $filter_val; 
    } 

    if($filter_type == 'sub_category'){ 
     $sub_category   = $filter_val; 
    } 

    if($filter_type == 'cat_operation'){ 
     $cat_operation   = $filter_val; 
    } 

    if($filter_type == 'room'){ 
     $room     = $filter_val; 
    } 

    $properties = Property::where(function($query) use ($state, $sub_category, $cat_operation, $room){ 

     if (isset($state)){ 
      $query->where('state_id', $state); 
     } 

     if (isset($sub_category)){ 
      $query->where('sub_category_id', $sub_category); 
     } 

     if (isset($cat_operation)){ 
      $query->where('cat_operation_id', $cat_operation); 
     } 

     if(isset($room)){ 

      $query->join('properties_control', function($join) use ($room) 
      { 
       if($room == 5){ 
        $join->on('properties.id', '=', 'properties_control.property_id') 
         ->where('properties_control.category_feature_item_id', '=', 75) 
         ->where('properties_control.category_feature_item_value', '>=', $room); 
       }else{ 
        $join->on('properties.id', '=', 'properties_control.property_id') 
         ->where('properties_control.category_feature_item_id', '=', 75) 
         ->where('properties_control.category_feature_item_value', '=', $room); 
       } 
      }); 

     } 

    })->paginate(20); 

的连接语句不运行在所有。

这可能包括一个连接闭包在一个地方像我想在这里做的闭包?还有另一种方法来完成这个?

回答

0

你的连接闭合不起作用的主要原因是因为它被封闭在先进的where闭合。

下面就来写上面的代码中laravel(右)的方式:

$filter_type= Input::get('filter_type',null); 
$filter_val= Input::get('filter_val',null); 

//Start the query builder 

$queryProperties = Property::newQuery(); 

//Switch on type of filter we received 
switch($filter_type) 
{ 
    case 'state' 
     $queryProperties->where('state_id',$filter_val); 
     break; 
    case 'sub_category': 
     $queryProperties->where('sub_category_id', $filter_val); 
     break; 
    case 'cat_operation': 
     $queryProperties->where('cat_operation_id', $filter_val); 
     break; 
    case 'room': 
     $queryProperties->join('properties_control', function($join) use ($filter_val) 
     { 
      if($room == 5){ 
       $join->on('properties.id', '=', 'properties_control.property_id') 
        ->where('properties_control.category_feature_item_id', '=', 75) 
        ->where('properties_control.category_feature_item_value', '>=', $filter_val); 
      }else{ 
       $join->on('properties.id', '=', 'properties_control.property_id') 
        ->where('properties_control.category_feature_item_id', '=', 75) 
        ->where('properties_control.category_feature_item_value', '=', $filter_val); 
      } 
     });   
     break; 
} 

$properties = $queryProperties->paginate(20);