0

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();
5
  • What isn't working? Commented May 9, 2018 at 17:01
  • I think there is a problem at ->orWhereIn. If I just try ->orWhere('id', '123'); this just runs fine. So there is something wrong on that part. Commented May 9, 2018 at 17:08
  • What's the result when you replace ->get() with ->toSql()? What's the content of $userIdSArray? Commented May 9, 2018 at 17:10
  • $userIdsArray = ['123', '456']; I don't get anything if i dd($rs) while using toSql() in the above Commented May 9, 2018 at 17:17
  • What Laravel version and database are you using? Commented May 9, 2018 at 17:19

1 Answer 1

0
$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 )]

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.