laravel查询生成器中的子查询

时间:2014-04-28 13:55:23

标签: laravel laravel-4

我怎样才能在laravel中执行此查询:

SELECT * FROM(
SELECT * FROM `tb_horario` where cod_funcionario="'.$cod.'" and deleted_at is null)
AS temp
where temp.motivo!='' or temp.validado=0

但有时我不必使用cod_funcionario,因为我的页面是一个过滤器

所以我有类似的事情:

if ($funcionario)
        {               
            $horariosQuery->where('cod_funcionario', $funcionario);
        }

我不知道如何在laravel中使用此子查询来执行此查询,就像我在sql中所做的那样。 thxx!

1 个答案:

答案 0 :(得分:1)

 $horariosQuery = $this->horario->with(array('funcionario', 'item_contabil'))
                              ->whereNull('deleted_at')
                              ->orderby('cod', 'ASC');

        if ($funcionario)
        {               
            $horariosQuery->where('cod_funcionario', $funcionario)
                          ->where(function ($query) {
                                $query->where('validado', 0)
                                      ->orWhere('motivo', '!=', '');
                            })
                          ->orderBy('data');
        }
        else
        {
            $horariosQuery->where('validado', 0)
                          ->orWhere('motivo', '<>', '')
                          ->orderBy('cod_funcionario');
        }  
相关问题