Laravel中的原始表达?

时间:2018-01-17 07:50:16

标签: sql laravel

我的sql代码是:

Select * from table where cond(condition)1 and con2 ...... and(conX or conY). 

我如何用原始表达式编写它。我不知道解决这个问题的关键工作。

更新:我的代码是:

//$builder->to sql =   Select * from table where cond(condition)1 and con2 ...... ;
    if(something != true) $builder->whereRaw(conX);  
else $builder->whereRaw((conX or ConY));
    //$builder->to sql =   Select * from table where cond(condition)1 and con2 ...... and (conX or conY);

好的,它有效,但我需要写2次conX,虽然它太长了。所以现在我想签约。

//$builder->to sql =   Select * from table where cond(condition)1 and con2 ...... and conX;
if(something = true) $builder->raw or do something;   (@)
//$builder->to sql =   Select * from table where cond(condition)1 and con2 ...... and (conX or conY);

问题在于:我应该在(@)行做什么来制作我期望的结果

1 个答案:

答案 0 :(得分:0)

尝试:

if (something == true) {
    $result = DB::table('table')
        ->selectRaw('*')
        ->where(condition1)
        ->where(condition2)
        ->get();
}

或者:

if (something == true) {
    $result = DB::table('table')
        ->select(DB::raw(YOUR SQL STATEMENT))
        ->get();
}

进一步阅读: https://laravel.com/docs/5.5/queries