1

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();
1

4 Answers 4

3

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();
Sign up to request clarification or add additional context in comments.

Comments

0

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();

1 Comment

Doesn't seem enough to help the author. If he already gave you an example of the code he has, try to work on that and see how you could improve it.
0

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

Comments

0

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();

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.