猿问

如何在 Laravel 5.8 中使用 IF & ELSE 检查 mysql 表是否为

如何在 Laravel 5.8 中找出数据库中的表是否为空。


我想使用这样的 if else 语句,但不起作用:


@foreach($accounts as $showaccounts)

@if($showaccounts->id == !NULL)

   <div class="list-item" data-id="item-11"><span class="w-40 avatar circle blue"> <img src="{{ $showaccounts->image }}" alt="."></span>

     <div class="list-body"><a href="app.message.php" class="item-title _500">{{ $showaccounts->username }}</a>

     </div>

   </div>

 @else

   <div class="no-result">

      <div class="p-4 text-center">No Results</div>

   </div>

@endif

@endforeach

我尝试了其他功能,但仍然无法正常工作。


@if($showaccounts->id === NULL)


@if(is_null($showaccounts->id))


@if(empty($showaccounts->id))


慕雪6442864
浏览 229回答 3
3回答

德玛西亚99

实际上,这就是您要执行的操作:@if($accounts->count())&nbsp; @foreach($accounts as $showaccounts)&nbsp; &nbsp;<div class="list-item" data-id="item-11"><span class="w-40 avatar circle blue"> <img src="{{ $showaccounts->image }}" alt="."></span>&nbsp; &nbsp; &nbsp;<div class="list-body"><a href="app.message.php" class="item-title _500">{{ $showaccounts->username }}</a>&nbsp; &nbsp; &nbsp;</div>&nbsp; &nbsp;</div>&nbsp;&nbsp; @endforeach@else&nbsp; <div class="no-result">&nbsp; &nbsp; <div class="p-4 text-center">No Results</div>&nbsp; </div>@endif但是如果你像下面这样构造它会更好:@forelse($accounts as $account)&nbsp; <div class="list-item" data-id="item-11"><span class="w-40 avatar circle blue"> <img src="{{ $account->image }}" alt="."></span>&nbsp; &nbsp; <div class="list-body">&nbsp; &nbsp; &nbsp; <a href="app.message.php" class="item-title _500">{{ $account->username }}</a>&nbsp; &nbsp; </div>&nbsp; </div>@empty&nbsp; <div class="no-result">&nbsp; &nbsp; <div class="p-4 text-center">No Results</div>&nbsp; </div>@endforelse循环变量通常采用复数形式,例如:accounts-account。

12345678_0001

在您的代码中,您检查了元素属性。但你想检查表。因此,您可以尝试获取元素集合的计数@if( $showaccounts->count() )&nbsp; &nbsp; @foreach($accounts as $showaccounts)&nbsp; &nbsp; &nbsp; &nbsp; <div class="list-item" data-id="item-11"><span class="w-40 avatar circle blue"> <img src="{{ $showaccounts->image }}" alt="."></span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="list-body"><a href="app.message.php" class="item-title _500">{{ $showaccounts->username }}</a>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; @endforeach@else&nbsp; &nbsp; <div class="no-result">&nbsp; &nbsp; &nbsp; &nbsp; <div class="p-4 text-center">No Results</div>&nbsp; &nbsp; </div>@endif
随时随地看视频慕课网APP
我要回答