【发布时间】:2021-09-29 11:53:19
【问题描述】:
我已尝试将此代码添加到我的 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
所以这意味着,如果用户没有任何 Wallet,则不要显示 id 为 theWallet 的 div。
但现在的问题是,它仍然显示 div 和复选框,但是用户没有任何钱包存储在数据库中。
当我dd($user_wallet) 时,我得到了这个:
Illuminate\Database\Eloquent\Collection {#2562 ▼
#items: []
}
也就是说,它是空的。但是为什么它仍然显示 div?
【问题讨论】:
-
试试
if(!$user_wallet->isEmpty())收集方法 -
empty(): Determine whether a variable is considered to be empty. A variable is considered empty if it does not exist or if its value equals false. empty() does not generate a warning if the variable does not exist.并且你有一个数组,即使数组为空,变量本身也不是, -
这能回答你的问题吗? Laravel check if collection is empty
标签: php laravel laravel-5.8