当我的foreach不包含任何值时,我试图不显示div。
<div class="box box-solid">
<div class="box-body">
<h4 style="background-color:#f7f7f7; font-weight: 600; font-size: 18px; text-align: center; padding: 7px 10px; margin-top: 0;">
MANUELS
</h4>
<div>
@foreach($filesFromUser as $file)
@if($file->category->name == 'Manuels')
<a href="{{ Url('/') . '/' . $file->file_url }}" class="btn btn-block btn-primary btn-flat" target="_blank">{{ $file->name }}</a>
@endif
@endforeach
</div>
</div>
</div>
问题在于foreach是在块“ box box-solid”中声明的,因此我不算()。解决办法是什么 ?我想隐藏“ box box-solid”。
非常感谢您!
答案 0 :(得分:2)
我认为这会有所帮助
<?php
$string = '';
foreach($filesFromUser as $file) {
if($file->category->name == 'Manuels') {
$string .= '<a href="{{ Url('/') . '/' . $file->file_url }}" class="btn btn-block btn-primary btn-flat" target="_blank">{{ $file->name }}</a>';
}
}
?>
<div class="box box-solid">
<div class="box-body">
<h4 style="background-color:#f7f7f7; font-weight: 600; font-size: 18px; text-align: center; padding: 7px 10px; margin-top: 0;">
MANUELS
</h4>
@if(!empty($string))
<div>{{ $string }}</div>
@endif
</div>
</div>
答案 1 :(得分:1)
除了在视图文件中执行此操作外,您还可以在将数据发送到视图之前,通过控制器方法过滤集合,
$filesFromUser = $filesFromUser->filter(function ($file) {
return $file->->category->name == 'Manuels';
});
return view('view-path', compact('filesFromUser'));
然后在您看来
@if($filesFromUser->count())
<div class="box box-solid">
<div class="box-body">
<h4 style="background-color:#f7f7f7; font-weight: 600; font-size: 18px; text-align: center; padding: 7px 10px; margin-top: 0;">
MANUELS
</h4>
<div>
@foreach($filesFromUser as $file)
@if($file->category->name == 'Manuels')
<a href="{{ Url('/') . '/' . $file->file_url }}" class="btn btn-block btn-primary btn-flat" target="_blank">{{ $file->name }}</a>
@endif
@endforeach
</div>
</div>
</div>
@endif
答案 2 :(得分:-1)
@if($filesFromUser->count() > 0)
<div class="box box-solid">
<div class="box-body">
<h4 style="background-color:#f7f7f7; font-weight: 600; font-size: 18px; text-align: center; padding: 7px 10px; margin-top: 0;">
MANUELS
</h4>
<div>
@foreach($filesFromUser as $file)
@if($file->category->name == 'Manuels')
<a href="{{ Url('/') . '/' . $file->file_url }}" class="btn btn-block btn-primary btn-flat" target="_blank">{{ $file->name }}</a>
@endif
@endforeach
</div>
</div>
</div>
@endif