4

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  
5
  • Check here. Commented Feb 16, 2019 at 23:03
  • Is recommendationWord1 name of column? Commented Feb 16, 2019 at 23:47
  • @Tpojka yes it is a column with the Users table Commented Feb 16, 2019 at 23:49
  • Then what @Davit left you there with change: 'recommendationWord1' instead of Auth::user()->recommendationWord1. First argument of whereIn($column_name, $array) method is name of column you want to check array values against. Check docs. Commented Feb 16, 2019 at 23:51
  • ok , also $array that contains column names as well just checking that you know that , im basically just comparing columns and finding those which r the same Commented Feb 17, 2019 at 0:05

3 Answers 3

2

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();
Sign up to request clarification or add additional context in comments.

3 Comments

Below is the error message , Column not found: 1054 Unknown column 'test1' in 'where clause' (SQL: select username from users where id != 7 and test1 in (value1, value2, value3)) . the Auth::user()->recommendationWord1 returns test1 which is correct as thats what is stored in that field
It seems to not be return or getting the values from the field value1, value2 and value3
@John argument should be name of column not value. I.e. 'recommendationWord1' instead of Auth::user()->recommendationWord1.
2

Try this:

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

5 Comments

"whereIn" array:3 [▼ 0 => "test1" 1 => "=" 2 => array:3 [▼ 0 => "word1" 1 => "word2" 2 => "word3" ] ]
That is the output of that statement , seems to not be getting word1 or in the example I gave value1 etc
How that is an output of this sentences? what did you dd() to get this result?
No sorry I meant to say it was in the error message console output , my bad.
I'll post in my answer how im outputting it
2

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

5 Comments

yes this look very good , It looks a lot neat. However the main thing that I am looking is basically instead of doing Auth::user()->recommendationWord1 = word1 , word2 , word3 , and so on repeatedly
It you look at @Davit answer below it shows it clearly but doesnt work correctly
let me know if it worked. Not sure if you saw it i edited my answer with your query possible solution
yes that solution does work but i would like it to be shortened down , thats my problem now. so like 1 orWhere statement would be great but not repeating Auth::user part
not sure if it can be achieved..at least i dont know it..what if you tried to combine my solution with the solution of @Davit ? using whereIn instead of orWhere

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.