Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

mufasaparadox's avatar

How to convert mysql query filtered by TIME() function to laravel query builder?

So far this is the MySQL query and it works perfectly. It filters records by matching passed value to TIME(fecha_hora):

 select nombre_departamento, categoria_respuesta, count(valor_evaluacion) from 
       detalle_evaluaciones JOIN departamentos ON departamentos.id = 
       detalle_evaluaciones.departamentos_id JOIN tipo_respuestas ON tipo_respuestas.id = detalle_evaluaciones.tipo_respuestas_id 
        where TIME(fecha_hora) = '06:13:00'

group by nombre_departamento, categoria_respuesta ORDER BY `tipo_respuestas`.`id` desc 

This is what I have tried in laravel:

$hora = $request->get('hora');
  $hora1 = date('H:i:s', strtotime("$hora"));
        $calif = DB::table ('detalle_evaluaciones')->select (DB::raw('departamentos.nombre_departamento,tipo_respuestas.categoria_respuesta,   detalle_evaluaciones.valor_evaluacion, COUNT(detalle_evaluaciones.valor_evaluacion) as cantidad'))
      ->join  ('departamentos','detalle_evaluaciones.departamentos_id','=','departamentos.id')
      ->join  ('tipo_respuestas','detalle_evaluaciones.tipo_respuestas_id','=','tipo_respuestas.id')
      ->whereRaw('(TIME(detalle_evaluaciones.fecha_hora)=?)',$hora1)

      ->groupby ('departamentos.nombre_departamento','tipo_respuestas.categoria_respuesta')

      ->orderby ('tipo_respuestas.id','DESC')
      ->paginate(10);

$calif will save all query data and return it and $hora1 is getting input time and passing its value to query builder where clause. It is not working at all. fecha_hora is of datetime type and it has both the date and time.

How to achieve this?

0 likes
8 replies
Ricardo's avatar

@mufasaparadox if you look at Raw Expressions

The whereRaw and orWhereRaw methods can be used to inject a raw where clause into your query. These methods accept an optional array of bindings as their second argument:

the bindings should be an array

1 like
mufasaparadox's avatar

@Ricardo But isn't it supposed to be optional? Or in any case, how should I pass the variable as an array?

Ricardo's avatar

@mufasaparadox si:

->whereRaw('(TIME(detalle_evaluaciones.fecha_hora)=?)', [$hora1])

opcional es si va el parámetro o no, no si es array o no.

1 like
mufasaparadox's avatar

@Ricardo quisiera decir que resolvio los problemas pero aun continua sin mostrarme los datos filtrados.

mufasaparadox's avatar

@Cronix That would be great to use. But I'm using laravel 5.5 Do you know a way to solve this?

mufasaparadox's avatar

Thank you @Cronix y @Ricardo. It seems the I was searching by a non existing time in my database haha. I was using a time from another database that I'm using as well as filter. Really dumb situations. Gracias ricardo, estaba buscando un dato que no existía en mi base de datos y por eso no mostraba ningun resultado. Como estoy usando ambas para hacer pruebas.

Please or to participate in this conversation.