简体   繁体   English

Laravel查询生成器Where OR whereIn数组

[英]Laravel Query Builder Where OR whereIn array

I am having some trouble in Laravel using the query builder to select users based on two where clauses. 我在Laravel中使用查询生成器基于两个where子句选择用户时遇到了一些麻烦。 The first where clause would contain multiple where statements to select a user based on some conditions. 第一个where子句将包含多个where语句,以便根据某些条件选择用户。 If this fails in the OR part I would like to check simply if the user ID is in the array. 如果在OR部分中失败,那么我想简单地检查用户ID是否在数组中。 Would be grateful for some advice on how I could achieve this. 感谢您提供一些有关如何实现此目标的建议。 Thanks. 谢谢。

$rs = DB::table('tblUser')
        ->select('name')
        ->where(function($q) {
            $q->where(function($q){
              $q->where('fid', '=', $this->argument('fid'))
                ->where('type', '<', 10)
            })
            ->orWhereIn('id', $userIdSArray);
        })
        ->get();
$userIdSArray = [ '2', '4', '5' ];
$rs           = DB::table( 'Users' )
                  ->select( 'name' )
                  ->where( function ( $q ) use ( $userIdSArray ) {
                      $q->where( function ( $q ) {
                          $q->where( 'fid', '=', $this->argument( 'fid' ) )
                            ->where( 'type', '<', 10 );
                       })
                       ->orWhereIn( 'id', $userIdSArray );
                  })
                  ->get();

I think you need to pass $userIdSArray in Query builder . 我认为您需要在Query builder中传递$ userIdSArray。 And You also forgot (;) after [->where( 'type', '<', 10 )] 并且您还忘记了[;> where('type','<',10)]之后的(;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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