Reputation: 59
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();
Upvotes: 1
Views: 380
Reputation: 240
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();
Upvotes: 0
Reputation: 15145
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
Upvotes: 0
Reputation: 2683
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();
Upvotes: 3
Reputation: 17
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();
Upvotes: 0