skip to Main Content

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

4

Answers


  1. Chosen as BEST ANSWER

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

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

  2. 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

    Login or Signup to reply.
  3. 
    $userName = "input text";
    
    $oneUser= User::query()->when($userName!="Chuck NORRIS",function($query) use($userName) {
     $query->where('name', $userName);
    })->get();
    
    Login or Signup to reply.
  4. you can use "whereNot" query like this:

     $oneUser= User::where('name', $userName)->whereNot($userName,"Chuck NORRIS")->first();
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search