简体   繁体   中英

How in laravel eloquent make query with multiple 'and'?

I have this query :

SELECT * 
FROM `groups` 
WHERE `status` = 1 
    AND `active` != 1 
    AND (`approved` != 1 OR `approved` IS NULL)

And I try this in query builder but don't know how to make it properly

Here is my query builder :

Group::where([['status', '=', 1], ['active', '!=', 1]])
    ->where([['approved', '!=', 1]])
    ->orWhereNull()
    ->get();

You should use where with Closure to group params. https://laravel.com/docs/5.7/queries#parameter-grouping

$data = Group::where(function($query){
        $query
            ->where('approved', '!=', 1)
            ->orWhereNull('approved');
    })
    ->where('status', 1)
    ->where('active', '!=', 1)
    ->get();

You can try something like:

$articles = \App\Article::where('foo', 'bar')
    ->where('color', 'blue')
    ->orWhere('name', 'like', '%John%')
    ->whereIn('id', [1, 2, 3, 4, 5])
    ->get();

try this one

 Group::where('status', 1)
    ->where('active', '!=', 1)
    ->where(function($query){
       $query->where('approved', '!=', 1)
            ->orWhereNull('approved')
    })->get();

use where in closure in laravel see

try this

Group::where('status', 1)->where('active', '!=', 1)->where('approved', '!=', 1)
->orWhere('status', 1)->where('active', '!=', 1)->where('approved', NULL)
->get();

or

$x = Group::where(function($q){
        $q->where('approved', '!=', 1)
          ->orWhereNull('approved')
        })
           ->where('status', 1)->where('active', '!=', 1)
           ->get();

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