我如何在 Laravel 中查询构建 Where IF 条件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39353472/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 14:25:26  来源:igfitidea点击:

How can i query build Where IF condition in Laravel

mysqllaravellaravel-5.2

提问by Gabriel Buzzi Venturi

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.

我正在我的 MySQL 编辑器中构建一个查询,但现在我想在 Laravel 5 的查询构建器 SQL 中转换下面的这个 SQL,有人可以帮助我吗?我在 Laravel 中搜索了很多关于 Where IF 条件的信息,但没有找到。

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

我在 Laravel 中构建直到这个

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

采纳答案by Gabriel Buzzi Venturi

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

我不知道这是否是正确的方法,但我像@vijaykuma 所说的那样使用 whereRaw 做到了。

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();

回答by Han Lim

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 =)

希望它有帮助 =)

回答by mcmacerson

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

如果您已经登陆这里尝试使 IF 作为 SELECT 的一部分工作,这可能会有所帮助:

$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

请记住: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.

如果(expr1,expr2,expr3)

如果 expr1 为 TRUE,则 IF() 返回 expr2。否则,它返回 expr3。