我有这个观点:
@foreach($recent_posts as $rp)
<div class="blogPostClassic">
<div class="blogThumb">
<ul class="bxsliderBlog">
@foreach($rp->photos as $rpt)
<li><img src="{{route('getPostImages', $rpt->image)}}" alt="" /></li>
@endforeach
</ul>
<div class="thumbBar"></div>
</div>
<table border="1" class="blogDetails">
<tr>
<td><i class="fa fa-calendar"></i> {{ Carbon\Carbon::parse($rp->created_at)->format('j F Y') }}</td>
<td><i class="fa fa-eye"></i> {{$rp->views}}</td>
<td><i class="fa fa-comments"></i> 20</td>
</tr>
</table><br>
<h1 style="font-size:calc(95% + 1.2vw);"><a href="#">{{$rp->title}}</a></h1>
<p>Posted by <a href="#">Sandra</a> in <a href="#">{{$rp->category->title}}</a></p>
<p></p><br/>
<a href="#" class="button blog">READ MORE</a>
</div>
<div class="dividerWidget"></div>
@endforeach
$rp->photos
包含属于每个帖子的图像数组。如何只从阵列中为每个帖子拍摄一张图像,这样我就不必过多使用轮播了?
索引方法:
public function index() {
$recent_posts = Post::with(['category', 'tags'])->orderBy('created_at', 'desc')->paginate(10);
return view('pages/home', compact('recent_posts', $recent_posts));
}
答案 0 :(得分:0)
只需像这样使用$i=0;
变量
<?php $i=0; ?>
@foreach($recent_posts as $rp)
........
<li><img src="{{route('getPostImages', $rp->photos[$i]->image)}}" alt="" /></li>
........
@endforeach
答案 1 :(得分:0)
我认为您可以使用first()
方法。所以在你的代码中它应该是:
@foreach($recent_posts as $rp)
......
<li><img src="{{route('getPostImages', $rp->photos->first()->image)}}" alt="" /></li>
.......
@endforeach
或者您可以先在变量中启动它:
@foreach($recent_posts as $rp)
<?php $photo = $rp->photos->first() ?>
......
<li><img src="{{route('getPostImages', $photo->image)}}" alt="" /></li>
.......
@endforeach