目前我有
@foreach (array_chunk($posts->all(), 3) as $row)
<div class="row">
@foreach ($row as $post)
<div class="col-md-4">
<h1>$post->title</h1>
<p>
{{ Str::limit($post->body, 50, link_to_route('posts.show', ' ...', $post->id)) }}
</p>
</div>
@endforeach
</div>
<div class="clearfix"></div>
@endforeach现在每个项目都有col-md-4,但我想做的是如下所示
@foreach (array_chunk($posts->all(), 3) as $row)
@for ($i = 0; $i < count($row); $i++)
<div class="row">
<div class="col-md-4">
<h1>{{ $row[$i]->title }}</h1>
<p>
{{ Str::limit($row[$i]->body, 50, link_to_route('posts.show', ' .....', $row[$i]->id)) }}
</p>
</div>
<div class="col-md-3">
<h1>{{ $row[$i]->title }}</h1>
<p>
{{ Str::limit($row[$i]->body, 50, link_to_route('posts.show', ' .....', $row[$i]->id)) }}
</p>
</div>
<div class="col-md-5">
<h1>{{ $row[$i]->title }}</h1>
<p>
{{ Str::limit($row[$i]->body, 50, link_to_route('posts.show', ' .....', $row[$i]->id)) }}
</p>
</div>
</div>
<div class="clearfix"></div>
@endfor
@endforeach现在每个项目都有我想要的类,但不幸的是,每一项都会重复3次。

,是为了达到上述目的而不被重复。
发布于 2014-12-26 09:35:57
所以,您总是希望行在3列中,按照这个顺序排列md-4、md-3、md-5列的宽度?
@foreach (array_chunk($posts->all(), 3) as $row)
<div class="row">
<?php $i = 0; ?>
@foreach ($row as $post)
<?php if ($i===0) echo '<div class="col-md-4">'; ?>
<?php if ($i===1) echo '<div class="col-md-3">'; ?>
<?php if ($i===2) echo '<div class="col-md-5">'; ?>
<h1>$post->title</h1>
<p>
{{ Str::limit($post->body, 50,
link_to_route('posts.show', ' ...', $post->id))
}}
</p>
</div>
<php $i++;?>
@endforeach
</div>
<div class="clearfix"></div>
@endforeach发布于 2014-12-26 09:23:51
以当前代码为例,向行<div class="col-md-4">添加一个条件,并根据文章的数量向div添加不同的类。你需要榜样吗?
发布于 2014-12-26 09:30:34
在超过3个帖子的情况下,您想要这个特定的命令:
@foreach (array_chunk($posts->all(), 3) as $row)
<div class="row">
@foreach ($row as $post)
@if (i%3 == 0)
<div class="col-md-4">
@elseif (i%3 == 1)
<div class="col-md-3">
@elseif (i%3 == 2)
<div class="col-md-5">
@endif
<h1>$post->title</h1>
<p>
{{ Str::limit($post->body, 50, link_to_route('posts.show', '...', $post->id)) }}
</p>
</div>
@endforeach
</div>
<div class="clearfix"></div>
@endforeachhttps://stackoverflow.com/questions/27655108
复制相似问题