How to get all the users except current logged in user in laravel eloquent

0 votes

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 do that? something like,

    User::where('id','!=',$currentUser->id)->get();
Dec 8, 2020 in Laravel by kartik
• 37,490 points
243 views

1 answer to this question.

0 votes

Hello @kartik,

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

$users = User::where('id', '!=', auth()->id())->get();

Hope it helps!!

Thank you!!

answered Dec 8, 2020 by Niroj
• 82,580 points

Related Questions In Laravel

0 votes
5 answers

How to get user's IP address in Laravel?

Looking at the Laravel API: Request::ip(); Internally, it uses the getClientIps the ...READ MORE

answered Dec 15, 2020 in Laravel by Roshni
• 10,420 points
17,567 views
0 votes
1 answer

How can we check the logged-in user info in Laravel?

Hey @kartik, yes we can keep track of ...READ MORE

answered Mar 20, 2020 in Laravel by Niroj
• 82,580 points
153 views
0 votes
1 answer

How to get all pending jobs in laravel queue on redis?

Hello @kartik, Here is the way I do ...READ MORE

answered Apr 6, 2020 in Laravel by Niroj
• 82,580 points
2,761 views
0 votes
1 answer

How to get the id when you're validating in the model Validation unique on update using laravel?

Hello @kartik, in Laravel's inbuilt auth system, the ...READ MORE

answered Sep 11, 2020 in Laravel by Niroj
• 82,580 points
1,184 views
+1 vote
1 answer

How to make anchor tag with routing using Laravel?

Hey @kartik, First you have to go to ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,580 points
5,638 views
0 votes
1 answer

What is redirection in Laravel?

Named route is used to give specific ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,580 points
652 views
0 votes
1 answer

How to install Laravel via composer?

Hello, This is simple you just need to ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,580 points
497 views
+1 vote
1 answer

What are named routes in Laravel and How can specify route names for controller actions?

Hey @kartik, Named routing is another amazing feature of ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,580 points
6,217 views
0 votes
1 answer

How to Get the Current URL Inside @if Statement in Laravel?

Hello @kartik, You can use: Request::url() to obtain the current ...READ MORE

answered Nov 11, 2020 in Laravel by Niroj
• 82,580 points
125 views
0 votes
1 answer

How to identify wheather the request is HTTP GET or HTTP POST in Laravel?

Hey, In order to identify the type of ...READ MORE

answered Mar 19, 2020 in Laravel by Niroj
• 82,580 points
215 views