Home > Mobile >  Laravel eloquent multiple WHERE
Laravel eloquent multiple WHERE

Time:10-28

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();

CodePudding user response:

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

CodePudding user response:


$userName = "input text";

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

CodePudding user response:

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

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