简体   繁体   中英

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. The first where clause would contain multiple where statements to select a user based on some conditions. If this fails in the OR part I would like to check simply if the user ID is in the array. 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 . And You also forgot (;) after [->where( 'type', '<', 10 )]

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