如何不显示空集合
How to not show empty collection
我已经尝试将此代码添加到我的 Blade 中:
@if(!empty($user_wallet))
<li class="mt-3" id="theWallet">
<div class="form-check">
<input class="form-check-input" type="checkbox" name="pay_wallet" value="1" id="thisWrapper">
<label class="form-check-label small mr-3" for="thisWrapper">
Pay with Wallet
</label>
</div>
</li>
@endif
所以这意味着,如果用户没有任何钱包,则不显示 ID 为 theWallet
的 div。
但现在的问题是,它仍然显示 div 和复选框,但是用户没有在数据库中存储任何钱包。
当我 dd($user_wallet)
时,我得到了这个:
Illuminate\Database\Eloquent\Collection {#2562 ▼
#items: []
}
也就是说,它是空的。但是为什么还是显示div?
您好,如果$user_wallet包含收集数据,那么您需要更改@if
条件。
替换你的
@if(!empty($user_wallet))
到
@if(!$user_wallet->isEmpty()) OR @if($user_wallet->isNotEmpty())
将isset()
函数与empty()
函数结合使用。
将您的 if 条件替换为
@if(!empty($user_wallet))
到
@if(!empty($user_wallet) && isset($user_wallet))
or
@if(isset($user_wallet))
isset()
函数是 PHP 中的内置函数,它检查变量是否已设置且不为 NULL。
此函数还检查声明的变量、数组或数组键是否有空值,如果有,isset()
returns false,在所有其他可能的情况下 returns true .
我已经尝试将此代码添加到我的 Blade 中:
@if(!empty($user_wallet))
<li class="mt-3" id="theWallet">
<div class="form-check">
<input class="form-check-input" type="checkbox" name="pay_wallet" value="1" id="thisWrapper">
<label class="form-check-label small mr-3" for="thisWrapper">
Pay with Wallet
</label>
</div>
</li>
@endif
所以这意味着,如果用户没有任何钱包,则不显示 ID 为 theWallet
的 div。
但现在的问题是,它仍然显示 div 和复选框,但是用户没有在数据库中存储任何钱包。
当我 dd($user_wallet)
时,我得到了这个:
Illuminate\Database\Eloquent\Collection {#2562 ▼
#items: []
}
也就是说,它是空的。但是为什么还是显示div?
您好,如果$user_wallet包含收集数据,那么您需要更改@if
条件。
替换你的
@if(!empty($user_wallet))
到
@if(!$user_wallet->isEmpty()) OR @if($user_wallet->isNotEmpty())
将isset()
函数与empty()
函数结合使用。
将您的 if 条件替换为
@if(!empty($user_wallet))
到
@if(!empty($user_wallet) && isset($user_wallet))
or
@if(isset($user_wallet))
isset()
函数是 PHP 中的内置函数,它检查变量是否已设置且不为 NULL。
此函数还检查声明的变量、数组或数组键是否有空值,如果有,isset()
returns false,在所有其他可能的情况下 returns true .