【问题标题】:Show icon depending on not empty value根据非空值显示图标
【发布时间】:2021-12-20 13:23:55
【问题描述】:

如果它下面有任何子菜单,我想显示这个下拉图标。亚洲在子菜单上有国家(其下的国家(中国/韩国),但美国没有。

下拉菜单形成

HTML

<div class="dropdown">
  <button type="button" class="btn btn-custom btn btn-outline-light text-light" 
  style="font-family: Times New Roman; font-weight: bold; font-style: italic;">Country</button>
    <div class="dropdown-content">
      @foreach($subcontinents as $subcontinent)
      <a><div class="dropdown2">
      @if(is_null($subcontinent->countries))
      <button class="dropbtn2">{{$subcontinent->subcontinent_name}}</button>
      @else
      <button class="dropbtn2">{{$subcontinent->subcontinent_name}}<i class="fa fa-caret-right pl-3" aria-hidden="true"></i></button>
      @endif
                      
     <div class="dropdown-content2">
      @foreach($subcontinent->countries as $division)
         <a href="" >{{$division->country_name}}</a>
      @endforeach
                       
    </div>
   </div>
  </a>
 @endforeach
 </div>
</div>

数据库

次大陆有很多国家。

如果一个次大陆有国家/不是空的国家,那么就会有下拉图标。

【问题讨论】:

  • 如果你有一个真正的一对多的雄辩关系,那么$subcontinent-&gt;countries 不会为空,它将是一个空集合,所以改用optional($subcontinent-&gt;countries)-&gt;isEmpty()) 会更安全

标签: laravel laravel-blade


【解决方案1】:
    <div class="dropdown">
      <button type="button" class="btn btn-custom btn btn-outline-light text-light" 
      style="font-family: Times New Roman; font-weight: bold; font-style: italic;">Country</button>
        <div class="dropdown-content">
          @foreach($subcontinents as $subcontinent)
          <a><div class="dropdown2">
            <button class="dropbtn2">{{$subcontinent->subcontinent_name}} 
              // You can check here if your values aren't empty or greater than 0. based on that you can show. you can also use !empty() here.
              @if(count($subcontinent->countries) > 0)<i class="fa fa-caret-right pl-3" aria-hidden="true"></i>@endif 
            </button>
          <div class="dropdown-content2">
           @foreach($subcontinent->countries as $division)
              <a href="" >{{$division->country_name}}</a>
           @endforeach   
        </div>
       </div>
      </a>
     @endforeach
     </div>
    </div>

【讨论】:

  • 请添加一些关于什么不起作用/修复了什么的解释,而不是仅仅转储代码。
  • 好的。已评论。
  • 好的,抱歉。但它现在正在使用解决方案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-11-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-08
  • 2014-02-18
  • 1970-01-01
相关资源
最近更新 更多