John
John

Reputation: 181

How to use AND operator in Laravel Query?

Select a user

where id =! Auth::user->id 

// this prevents the query returning the current user logged in 

where Auth::user->value1 ,'=', 'value1'  ||   'value2' || 'value3' 

So in english , find the a username where id is not equal to the currently logged in user id and where authenticated user's value1 column equals another user's value1 or value2 or value 3column

Below is a statement, i have done which isn't working correctly however it does work without the OR operator

where(Auth::user()->recommendationWord1, '=' , 'value1') 

this statement is an example of the format and contains what I have tried.

$Query = DB::table('users')
    ->select('username')
    ->where('id', '!=', Auth::user()->id)
    ->whereIn(Auth::user()->recommendationWord1 , '=', 'value1', '||' , 'value2', '||' , 'value3'
    ->get();

Below is the code for my HTML page and how i am posting it

@foreach ($Query as $selectedUser)

    Code formatting<p> {{ $selectedUser->username}}</p>  

@endforeach  

Upvotes: 4

Views: 8871

Answers (3)

Davit Zeynalyan
Davit Zeynalyan

Reputation: 8618

where in structure is ->whereIn('attribute', ['val1', 'val2', ...]) In your case

DB::table('users')
    ->select('username')
    ->where('id', '!=', Auth::user()->id)
    ->whereIn(Auth::user()->recommendationWord1 , ['value1', 'value2', 'value3'])
    ->get();

Upvotes: 2

daenerysTarg
daenerysTarg

Reputation: 352

Have you tried the grouping parameter?

$Query = DB::table('users')
        ->select('username')
        ->where('id', '!=', Auth::user()->id)
        ->where(function($query){
              $query->where(Auth::user()->recommendationWord1, '=', 'value1')
              ->orWhere(Auth::user()->recommendationWord1, '=', 'value2')
              ->orWhere(Auth::user()->recommendationWord1, '=', 'value3');
              })
         ->get();

See the example from the documentation:

Sometimes you may need to create more advanced where clauses such as "where exists" clauses or nested parameter groupings. The Laravel query builder can handle these as well. To get started, let's look at an example of grouping constraints within parenthesis:

DB::table('users')
            ->where('name', '=', 'John')
            ->where(function ($query) {
                $query->where('votes', '>', 100)
                      ->orWhere('title', '=', 'Admin');
            })
            ->get();
enter code here

As you can see, passing a Closure into the where method instructs the query builder to begin a constraint group. The Closure will receive a query builder instance which you can use to set the constraints that should be contained within the parenthesis group. The example above will produce the following SQL:

select * from users where name = 'John' and (votes > 100 or title = 'Admin')

https://laravel.com/docs/5.7/queries#where-clauses

Upvotes: 2

Kenny Horna
Kenny Horna

Reputation: 14271

Try this:

$users = User
            ::where('id', '<>', auth()->id())
            ->whereIn(auth()->user()->recommendationWord1, ['value1', 'value2', 'value3']) 
            ->get()

Upvotes: 2

Related Questions