blade laravel 的条件
blade condition for laravel
我想在 foreach 中创建一个条件,当在数据库中找不到任何信息时显示一条消息,但当它找不到任何信息时不显示任何信息。
有人可以向我解释我哪里错了吗?请
控制器:
public function mycharacters()
{
$id = Auth::id();
$user = Player::where('account_id', $id)->get();
return view('user.characters')->with('user', $user);
}
Blade
<h1 class="text-light">My Characters</h1>
@foreach ($user as $u)
@if ($u->name !== "")
<h1 class="text-light">{{ $u->name }}</h1>
@else
<h1 class="text-light">I have not found anything!</h1>
@endif
@endforeach
您似乎希望 $user
为空,因此 foreach 没有任何可循环的内容。
相反,您应该遍历用户或在空时显示一条消息
@forelse ($user as $u)
<h1 class="text-light">{{ $u->name }}</h1>
@empty
<h1 class="text-light">I have not found anything!</h1>
@endforelse
@empty
块将在被循环的变量为空的情况下呈现。
我想在 foreach 中创建一个条件,当在数据库中找不到任何信息时显示一条消息,但当它找不到任何信息时不显示任何信息。 有人可以向我解释我哪里错了吗?请
控制器:
public function mycharacters()
{
$id = Auth::id();
$user = Player::where('account_id', $id)->get();
return view('user.characters')->with('user', $user);
}
Blade
<h1 class="text-light">My Characters</h1>
@foreach ($user as $u)
@if ($u->name !== "")
<h1 class="text-light">{{ $u->name }}</h1>
@else
<h1 class="text-light">I have not found anything!</h1>
@endif
@endforeach
您似乎希望 $user
为空,因此 foreach 没有任何可循环的内容。
相反,您应该遍历用户或在空时显示一条消息
@forelse ($user as $u)
<h1 class="text-light">{{ $u->name }}</h1>
@empty
<h1 class="text-light">I have not found anything!</h1>
@endforelse
@empty
块将在被循环的变量为空的情况下呈现。