繁体   English   中英

Laravel从2个表中获取数据,其中不为null

[英]Laravel get data from 2 tables where not null

我正在尝试从两个不同的表中获取一些数据,

所以现在看起来确实像这样:

图片1

但是我不希望显示空白字段(Globale-Moderator,Moderator和Proef-Moderator)。 我该怎么做?

控制器:

public function ForumTeam()
    {
        $roles = Role::where('id', '>', '4')->orderBy('id', 'DESC')->get();
        return View::make('team')->with('roles', $roles);
    }

视图:

<div class="panel-group col-sm-offset-1 col-sm-10">
  @foreach($roles as $role)
    <div class="panel panel-default">
      <div class="panel-heading">
        <h3 class="panel-title">{{ $role->name }}</h3>
      </div>
      <div class="panel-body">
        <ul class="list-group">

          @foreach($role->user as $user)
            <li class="list-group-item">
              <img src="{{ Config::get('app.url') }}/public/img/avatar.jpg" style="float:left;margin-right:15px;padding-bottom:5px;" class="img-circle" alt="{{ $user->username }}" width="75" height="75">
              <h4 style="color:{{ $role->colour }};"><strong>{{ $user->username }}</strong></h4>
              <p>{{ $user->usertitle }}</p>
            </li>
          @endforeach

        </ul>
      </div>
    </div>
  @endforeach
</div>

因此,我想隐藏未填写的字段,该怎么办?

我的另一个选择是在“空”字段内显示一些文本。 就像“没有人拥有这个等级”。

谢谢!

您可以/应该能够使用:

@foreach($roles as $role)

   @if($role->user != null || !empty($role->user->name))

   @endif

@endforeach

然后,您可以检查role->user是否为NULL或角色名称不为空。WHERE name是角色的名称,即“ Admin”,“ Moderator”

或者,尝试:

  @foreach($roles as $role)

    @if(count($role->user) >= 1)
       // Do stuff
    @endif

  @endforeach

由于您获得的User具有一对多关系,因此将有不止一个用户。

您可以在第一个@foreach之后添加if语句,以检查角色是否有用户,

@foreach($roles as $role)
    @if(isset($role->user))
    ...
    @endif
@endforeach

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM