usrNotFound
usrNotFound

Reputation: 2800

Laravel Eloquent WHERE and AND

I have comment table with id, privacy(0,1,2), comment_body columns

I am using Larvael 4.2.x Eloquent to get the result. What I am trying to get is all the comment with the privacy of 0 and 1 from one user.

What I have tried?

Option 1

$user  = User::find(1);

//query will give me comment for privacy = 0
$comment_for_user = $user->comments()->where('privacy','0');

Option 2

//this wont return anything
$comment_for_user = $user->comments()->where('privacy','0')
                                    ->where('privacy',1);

Option 3

//return the result but gets post form all users 
$comment_for_user = $user->comments()->where('privacy','0')
                                    ->Orwhere('privacy',1);

Thanks you for your help.

Upvotes: 2

Views: 105

Answers (1)

Sh1d0w
Sh1d0w

Reputation: 9520

You can use whereIn.

$comment_for_user = $user->comments()->whereIn('privacy', array(0, 1))->get();

You can find more useful methods in the documentation.

Upvotes: 3

Related Questions