Get all the users except current logged in user in laravel eloquent

后端 未结 4 1843
无人共我
无人共我 2021-01-31 15:44

I am doing:

    User::all();

to get all the users from users table. I want to select all the users except current logged in user. How should I

相关标签:
4条回答
  • 2021-01-31 16:22

    If you are using the Auth helper, use this.

    User::where('id', '!=', Auth::user()->id)->get();
    
    0 讨论(0)
  • 2021-01-31 16:35

    You can get the current user's id with auth()->id(). Then pass that to the query:

    $users = User::where('id', '!=', auth()->id())->get();
    
    0 讨论(0)
  • 2021-01-31 16:36

    Using except() will accomplish the same thing a little more fluently:

    $users = User::all()->except(Auth::id());
    

    ...or, since you already have the User id:

    $users = User::all()->except($currentUser->id);
    
    0 讨论(0)
  • 2021-01-31 16:45
    Use Auth;
    Use App\User;
    
    public function index()
    $id = Auth::id();
    $result = User::where('id', '!=', $id)->get();
    dd($result);
    
    0 讨论(0)
提交回复
热议问题