我正在努力显示一些内容,具体取决于数组属性是否具有值。 如果文章有标题,我想显示整篇文章的内容,如果不是,我想展示其他内容。但是,它不起作用。 else语句中的代码不会被执行。
这里有什么问题?
我的代码是:
@foreach($restaurantmenue as $daily)
@foreach($daily->articles as $menue)
@if(!empty($menue->title))
<div class="card card-horizontal">
<div class="row">
<div class="col-md-5">
<div class="image" style="background-image: url({{asset('images/frontend/profile/profileHero.jpg')}}); background-size: cover; background-position: center center;">
<img src="{{asset('images/frontend/profile/profileHero.jpg')}}" alt="..." style="display: none;">
<div class="filter filter-azure">
<button type="button" class="btn btn-neutral btn-round">
<i class="fa fa-heart"></i> SMÄCKT MIR
</button>
</div>
</div>
</div>
<div class="col-md-7">
<div class="content">
<p class="category text-info">
<i class="fa fa-trophy"></i> Best of
</p>
<a class="card-link" href="#">
<h4 class="title">{{$menue->title}} </h4>
</a>
<a class="card-link" href="#">
<p class="description">{{$menue->body}}</p>
<br />
{{$menue->price}} €
</a>
<div class="footer">
<div class="stats">
<a class="card-link" href="#">
<i class="fa fa-male"></i> {{$restaurant->name}}
</a>
</div>
<div class="stats">
<a class="card-link" href="#">
<i class="fa fa-comments"></i> 23 Comments
</a>
</div>
<div class="stats">
<a class="card-link" href="#">
<i class="fa fa-heart"></i> 231 Likes
</a>
</div>
</div>
</div>
</div>
</div>
</div>
@else
No Articles for Today
@endif
@endforeach
@endforeach
答案 0 :(得分:11)
如果您在laravel中使用ORM获取数据,如
$data = \App\Model::get();
然后您可以检查$data
是否为空,如下所示:
@if(!$data->isEmpty())
// $data is not empty
@else
// $data is empty
@endif
参考:Check Here
但是如果您使用find
或findOrFail
,则可以将数组检查为
$data = \App\Model::findOrFail($id);
@if(!empty($data))
// $data is not empty
@else
// $data is empty
@endif
答案 1 :(得分:0)
好吧,如果文章不为空,则执行代码。但是else语句中的代码仍未执行。我仍然无法显示&#34;没有文章&#34;消息它没有可用的文章
@if(!$restaurantmenue->isEmpty())
@foreach($restaurantmenue as $daily)
@foreach($daily->articles as $menue)
<div class="card card-horizontal">
<div class="row">
<div class="col-md-5">
<div class="image" style="background-image: url({{asset('images/frontend/profile/profileHero.jpg')}}); background-size: cover; background-position: center center;">
<img src="{{asset('images/frontend/profile/profileHero.jpg')}}" alt="..." style="display: none;">
<div class="filter filter-azure">
<button type="button" class="btn btn-neutral btn-round">
<i class="fa fa-heart"></i> SMÄCKT MIR
</button>
</div>
</div>
</div>
<div class="col-md-7">
<div class="content">
<p class="category text-info">
<i class="fa fa-trophy"></i> Best of
</p>
<a class="card-link" href="#">
<h4 class="title">{{$menue->title}} </h4>
</a>
<a class="card-link" href="#">
<p class="description">{{$menue->body}}</p>
<br />
{{$menue->price}} €
</a>
<div class="footer">
<div class="stats">
<a class="card-link" href="#">
<i class="fa fa-male"></i> {{$restaurant->name}}
</a>
</div>
<div class="stats">
<a class="card-link" href="#">
<i class="fa fa-comments"></i> 23 Comments
</a>
</div>
<div class="stats">
<a class="card-link" href="#">
<i class="fa fa-heart"></i> 231 Likes
</a>
</div>
</div>
</div>
</div>
</div>
</div>
@endforeach
@endforeach
@else
no article
@endif
答案 2 :(得分:-1)
希望这对你有所帮助。
$processes = [1, 2, 4, 5];
$maintenance = [1, 2, 3, 4, 5];
$array_value = function ($value, $array) {
$str = "$value";
if (in_array($str, $array)) {
echo 5;
} else {
echo 1;
}
};
foreach ($maintenance as $m) {
$array_value ($m, $processes);
}
基本上我正在比较两个数组,如果在另一个数组上找到数据,则显示5