user3398940
user3398940

Reputation: 135

Eloquent query builder replacement of and

Hi I want to build query with eloquent.

DB name friends with 4 rows id,friend1, friend2 and accepted.

So I want to create something like this

$count = Friends::where('friend1', '=', '$user1')->and('accepted', '=', '1')->orWhere('friend2', '=', '$user2')->and('accepted', '=', '1')->get();

Should I replace 'and' with 'where'. And if I do that it will work normally as with 'and'?

Upvotes: 0

Views: 400

Answers (1)

Antonio Carlos Ribeiro
Antonio Carlos Ribeiro

Reputation: 87719

I think this is a clear way of looking at it:

$count = Friends::where(function($query){
             $query->where('friend1', '=', '$user1')
                   ->where('accepted', '=', '1')
         })->orWhere(function($query){
             $query->where('friend2', '=', '$user2')
                   ->where('accepted', '=', '1')
         })->get();

Upvotes: 1

Related Questions