0

home blade

<div class="card-header">Total Users: {{ $users->count() }} </div>

<div class="card-body">
    @if (session('status'))
        <div class="alert alert-success" role="alert">
            {{ session('status') }}
        </div>
    @endif

    <table class="table table-dark table-striped">
        <tr>
            <th>SL</th>
            <th>Name</th>
            <th>Email</th>
            <th>Created at</th>
        </tr>
        @foreach($users as $user)
            <tr>
                <td>{{$loop->index +1}}</td>
                <td>{{ $user-> name}}</td>
                <td>{{$user->email}}</td>
                <td>{{$user->created_at->diffForHumans()}}</td>
            </tr>
     @endforeach
   </table>
</div>

HomeController

public function index()
{
    $users =  User::all();
    return view('home', compact('users'));
}

When I am logged in I want to see all the other users information in a table other than the logged-in user in Laravel. How can I accomplish that?

Don't Panic
  • 13,965
  • 5
  • 32
  • 51
Farahnaz A
  • 3
  • 2
  • 6
  • Currently there is no built-in way to do this in Laravel. But you can try this: https://laracasts.com/discuss/channels/laravel/find-out-all-logged-in-users He uses redis to record logged in users. – Puckwang Feb 08 '20 at 00:57

1 Answers1

2

While the accepted answer works, a nicer approach would be to make use of Laravel's except() Collection method:

The except method returns all items in the collection except for those with the specified keys

Your query then just returns all users except the currently logged in user to your view. No logic in your view, no other changes required:

public function index()
{
    $users =  User::all()->except(Auth::id);
    return view('home', compact('users'));
}
Don't Panic
  • 13,965
  • 5
  • 32
  • 51