Laravel query builder where and or where
Asked Answered
T

1

10

I want to build a query something like this:

select * from users where id=1 AND address="USA" AND (status="active" OR status="pending")

how can i do that with laravel query builder?

please help. Thanks!

Truckle answered 27/7, 2017 at 19:38 Comment(0)
J
25

Try to find complex query examples with Laravel Query Builder. And maybe this can help a little:

DB::table('users')
    ->where('id', 1)
    ->where('address', 'USA')
    ->where(function($query) {
        $query->where('status', 'active')
            ->orWhere('status', 'pending');
    })->get();

or:

DB::table('users')
    ->where([
        ['id', 1],
        ['address', 'USA'],
    ])
    ->where(function($query) {
        $query->where('status', 'active')
            ->orWhere('status', 'pending');
    })->get();
Jospeh answered 27/7, 2017 at 20:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.