Reputation: 14588
I have a project table like this-
I want to select all data with company ID 0 and 2 (company ID may be 1,3,3,4,5,6,...).
So, what I have done in Laravel Query Builder is -
DB::table( 'project')
->where('project.is_archived', '=', 0)
->where('project.companyID', '=', 0)
->orwhere('project.companyID', '=', 2)
->get;
But it is not working.
Can anyone help please?
Upvotes: 1
Views: 92
Reputation: 8149
In addition to mimo's solution, you can also combine statements using callbacks or the special where
methods.
So something like:
DB::table('project')
->where('is_archived', 0)
->orWhere(function(Builder $query) {
$query
->where('companyID', 0)
->where('companyID', 2)
});
would create this statement:
SELECT * FROM project WHERE (is_archived = 0) AND (companyID = 0 OR companyID = 2)
You could also do that with a simple ->where(function($query) {})
where you change the $boolean
argument of the encapsulated where()
statements to 'or' (which can allow for much more complex statements)
See the Laravel 5.1 Docs for Parameter Grouping
Upvotes: 1
Reputation: 13562
DB::table('project')
->where('is_archived', 0)
->whereIn('companyID', [0, 2])
->get();
This should work :)
Upvotes: 2