如何使用“ ||”在选择查询laravel

时间:2019-09-26 12:57:31

标签: laravel

我需要有关代码的帮助。

我试图编写这样的代码

$selectint = \DB::table('forums')->where([['category', '=', $f]])->orderByRaw('id ASC')->get();

但是我需要|| mysqli中的函数示例:
SELECT * FROM forums WHERE category = ? && (community = ? || community = ?) ORDER BY forum_id ASC

请帮助

3 个答案:

答案 0 :(得分:1)

您可以将闭包传递给where函数

->where(function($query){ 
    $query->whereCommunity('this')
          ->orWhereCommunity('that');
    )})

答案 1 :(得分:1)

||是OR,因此您可以将其表达为

dd/mm/yyyy hh:mm:ss

在Eloquent(Laravel的ORM)中,它类似于:

SELECT * 
FROM forums 
WHERE category = ? 
AND community IN (?, ?) 
ORDER BY forum_id ASC

查看有关QueryBuilders的Laravel官方文档:https://laravel.com/docs/5.8/queries

答案 2 :(得分:0)

尝试一下:

$selectint = DB::table('forums')->where('category','=','value')
->where(function($query){ 
     $query->where('community','=','C2')
      ->orWhere('community','=','C1');
})
->orderBy('forum_id')->get();