php - Laravel: 在闭包的地方加入闭包。 Eloquent

标签 php mysql laravel eloquent query-builder

我正在尝试使用过滤器搜索功能在我的“属性”表中选择一些注册表。

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

Controller :

        $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);

连接语句根本没有运行。

是否可以像我在这里尝试做的那样在 where 闭包中包含一个 join 闭包?还有另一种方法可以做到这一点吗?

最佳答案

您的 join 闭包不起作用的主要原因是它包含在高级 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); 

关于php - Laravel: 在闭包的地方加入闭包。 Eloquent ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33561534/

相关文章:

php - 如何防止 PHP 中的 SQL 注入(inject)?

php - FirePHP 是否受到重定向时清除 Firebug 控制台的限制?

php - 使用 pecl oauth 为 LTI 结果服务构建 body 签名的 oauth xml 请求

mysql - 如果另一个表mysql中存在ID,如何返回标志

mysql - 如何从相互引用的两个表中删除外键约束?

php - 在 Laravel 中获取字段的最新不同值

php - 如何在不换行的情况下添加这些多个 apply_filters(content, value)?

php - 如何与节点共享 Laravel session ?

php - 使用 hasColumns laravel 更改数据库表

mysql - 我想捕获文档的首次接收日期。请帮我查询一下吗?