[英]Laravel get data from 2 tables where not null
I'm trying to get some data from two different tables, 我正在尝试从两个不同的表中获取一些数据,
So it does look like this now: 所以现在看起来确实像这样:
But I don't want the empty fields (Globale-Moderator, Moderator and Proef-Moderator) to be shown. 但是我不希望显示空白字段(Globale-Moderator,Moderator和Proef-Moderator)。 How can I do this whit this code;
我该怎么做?
Controller: 控制器:
public function ForumTeam()
{
$roles = Role::where('id', '>', '4')->orderBy('id', 'DESC')->get();
return View::make('team')->with('roles', $roles);
}
View: 视图:
<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>
So I want to hide the not filled in fields, how should I do dis propperly? 因此,我想隐藏未填写的字段,该怎么办?
My other option is to show some text inside of the 'empty' field. 我的另一个选择是在“空”字段内显示一些文本。 Like 'There is nobody with this rank'.
就像“没有人拥有这个等级”。
Thanks! 谢谢!
You can/should be able to use: 您可以/应该能够使用:
@foreach($roles as $role)
@if($role->user != null || !empty($role->user->name))
@endif
@endforeach
You can then check to see if the role->user
is NOT NULL OR the name of the role is not empty.WHERE name
is the name of the role, ie "Admin", "Moderator" 然后,您可以检查
role->user
是否为NULL或角色名称不为空。WHERE name
是角色的名称,即“ Admin”,“ Moderator”
Alternatively, try: 或者,尝试:
@foreach($roles as $role)
@if(count($role->user) >= 1)
// Do stuff
@endif
@endforeach
Since you are getting User
which has a one-to-many relationship, therefore there will be more than one user. 由于您获得的
User
具有一对多关系,因此将有不止一个用户。
You could add an if statement after the first @foreach
, to check if the role has users, 您可以在第一个
@foreach
之后添加if语句,以检查角色是否有用户,
@foreach($roles as $role)
@if(isset($role->user))
...
@endif
@endforeach
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.