0

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!

6
  • Did you find this? stackoverflow.com/questions/16815551/… Commented Apr 28, 2014 at 13:58
  • Yeah I'm trying to do something like, but I'm not sure where I put that filter, in whereIn before the subquery or inside the subquery Commented Apr 28, 2014 at 14:01
  • Also, I don't understand why you need a subquery here. Why can't you AND temp.motivo!='' or temp.validado=0? Commented Apr 28, 2014 at 14:03
  • because if I dont filter "cod_funcionario" I select extra data, because it can be or "validado" or "motivo!=''" and if I dont select "funcionario" before I get data from other employee because of the OR Commented Apr 28, 2014 at 14:16
  • Then what you need to read is this: stackoverflow.com/questions/16995102/… Commented Apr 28, 2014 at 14:19

1 Answer 1

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');
        }  
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.