Lucas Campos
Lucas Campos

Reputation: 515

Subquery in laravel query builder

How could I do this query in 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

but sometimes I dont have to use cod_funcionario because is a filter in my page

so I've something like:

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

I dont know how to do this query with this subquery in laravel like I did in sql. thxx!

Upvotes: 0

Views: 681

Answers (1)

Lucas Campos
Lucas Campos

Reputation: 515

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

Upvotes: 1

Related Questions