Laravel 6 ErrorException 未定义变量:用户 (...home.blade.php)

Laravel 6 ErrorException Undefined variable: user (...home.blade.php)

我试图在 home.blade.php 中显示数据库中每个用户的姓名。我收到此错误:

ErrorException
Undefined variable: user (View...home.blade.php)

$user is undefined
Make the variable optional in the blade template. Replace {{ $user }} with {{ $user ?? '' }}

下面是home.blade.php中的代码:

<!-- SQL statement select all profiles except logged user -->

foreach ($user as $users)
   <h6>{{ $user->name }}</h6>
endforeach

下面是HomeController.php

中的代码
public function index()
    {
        $users = Users::where('id', '!=', auth()->id())->get();

        return view('home');
    }

在你的HomeController.php

return view('home', ['users' => $users]);

你的 foreach 应该是:

@foreach($users as $user)
     <h6>{{ $user->name }}</h6>
@endforeach