S M Abrar Jahin
S M Abrar Jahin

Reputation: 14588

LAravel Query Builder - A little different Where Query

I have a project table like this-

asdf

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

Answers (2)

Josh
Josh

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

cre8
cre8

Reputation: 13562

 DB::table('project')
    ->where('is_archived', 0)
    ->whereIn('companyID', [0, 2])
    ->get();

This should work :)

Upvotes: 2

Related Questions