Elodie Muller
Elodie Muller

Reputation: 215

Laravel eloquent multiple WHERE

I'm new in php/laravel.

I'm trying to get one user with the userName of the user connected except if it's "Chuck NORRIS"

This, is working. I'm getting the user connected information

  $oneUser= User::where('name', $userName)->get();

This, is not working. I'm getting the user Chuck NORRIS if he's the one connected

 $oneUser= User::where('name', $userName)->where($userName, '!=', "Chuck NORRIS")->get();

Upvotes: 1

Views: 1442

Answers (4)

you can use "whereNot" query like this:

 $oneUser= User::where('name', $userName)->whereNot($userName,"Chuck NORRIS")->first();

Upvotes: 0

Elodie Muller
Elodie Muller

Reputation: 215

This is what i've done and what is working :

$oneUser = User::where('name', '!=', "Chuck NORRIS")->where('name', $userName)->get();

Upvotes: 0

Ramil Huseynov
Ramil Huseynov

Reputation: 371


$userName = "input text";

$oneUser= User::query()->when($userName!="Chuck NORRIS",function($query) use($userName) {
 $query->where('name', $userName);
})->get();

Upvotes: 0

Delano van londen
Delano van londen

Reputation: 416

if($userName != "Chuck NORRIS") {
  $oneUser= User::where('name', $userName)->get();
}
else{
return back();
}

this should work, you check is the $userName is Chuck NORRIS if not, then run the query otherwise return back

Upvotes: 1

Related Questions