简体   繁体   中英

How can i query build Where IF condition in Laravel

i'm build a query in my MySQL editor, but now i want to transform this SQL below in a query builder SQL in Laravel 5, can someone help me? i searched a lot about Where IF condition in Laravel, but not was found.

select *
from eventos
where inicio = '2016-09-06 09:41'
and recorrencia = 0
or recorrencia = 1 
and 
(
    if (recorrencia_tipo = 'semanal', recorrencia_intervalo, 7) LIKE concat('%', weekday('2016-09-06 00:00:00'), '%')
        or
    if(recorrencia_tipo = 'mensal', recorrencia_intervalo, 0) = day('2016-09-06 09:41')
        or
    (
        if (recorrencia_tipo = 'anual', substring_index(recorrencia_intervalo, '/', 1), 0) = DAY('2016-09-06 09:41')
            or 
        if (recorrencia_tipo = 'anual', substring_index(recorrencia_intervalo, '/', -1), 0) = MONTH('2016-09-06 09:41')
    )
)

I builded until this in Laravel

Evento::where('inicio', '2016-09-06 09:41')
            ->where('recorrencia', 0)
            ->orWhere('recorrencia', 1)
            ->where('recorrencia_tipo', function ($query) {
            })->get();

I don't know if this is the right way, but i did it using whereRaw like @vijaykuma said.

Follow the query

$eventos = Evento::where('inicio', $data)
            ->where('recorrencia', 0)
            ->orWhere('recorrencia', 1)
            ->whereRaw("
                IF (recorrencia_tipo = 'semanal', recorrencia_intervalo, 7) LIKE concat('%', weekday('{$data}'), '%')
                    OR
                IF (recorrencia_tipo = 'mensal', recorrencia_intervalo, 0) = DAY('{$data}')
                    OR  
                (
                    IF (recorrencia_tipo = 'anual', substring_index(recorrencia_intervalo, '/', 1), 0) = DAY('{$data}')
                        AND
                    IF (recorrencia_tipo = 'anual', substring_index(recorrencia_intervalo, '/', -1), 0) = MONTH('{$data}')
                )
            ")->get();

Try this:

Evento::where('inicio', '2016-09-06 09:41')
        ->where('recorrencia', 0)
        ->orWhere('recorrencia', 1)
        ->where('recorrencia_tipo', function ($query) {
          if(condition){
            $query->select('column')->from('table')->where('where clause');
          }
          else{
            $query->select('column')->from('table')->where('where clause');
          }
        })
        ->get();

Hope it helps =)

If you've landed here trying to make IF work as part of the SELECT, this might help:

$query = DB::table('mytable')
    ->select(DB::raw('id,title,IF(some_column = 1, some_column,null) as some_column'))
    ->get();

Keep in mind: https://dev.mysql.com/doc/refman/5.7/en/control-flow-functions.html

IF(expr1,expr2,expr3)

If expr1 is TRUE, IF() returns expr2. Otherwise, it returns expr3.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM