我正在尝试显示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.hp:中的代码
<!-- 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($users as $user)
<h6>{{ $user->name }}</h6>
@endforeach