I am building a blog in laravel but I found a problem in my blog template, from the file of routes I send by compact the variable post:
Route::get('/blog', function () {
$posts = App\Post::latest('published_at')->get();
return view('blog', compact('posts'));
});
and from the view I implement a @foreach
:
<!-- Blog -->
<div class="section blog-posts" id="blog-section">
@foreach($posts as $post)
<div class="row">
<div class="col col-m-12 col-t-6 col-d-6">
<div class="blog_item animated">
<div class="image">
<a href="blog-page.html"><img src="mcard/images/blog/blog1.jpg" alt="" /></a>
</div>
<div class="content-box">
<div class="i_title">
<div class="icon"><strong>{{ $post->published_at->format('d') }}</strong> {{ $post->published_at->format('M') }}</div>
</div>
<div class="category_bts">
@foreach($post->tags as $tag)
<a href="#" class="category">#{{ $tag->name }}</a>
@endforeach
</div>
<a href="blog-page.html" class="name">{{ $post->title }}</a>
<p>
{{ $post->excerpt }}
</p>
<a href="blog-page.html" class="btn btn_animated"><span class="circle">Leer más</span></a>
</div>
</div>
</div>
<div class="col col-m-12 col-t-6 col-d-6">
<div class="blog_item animated">
<div class="image">
<a href="blog-page.html"><img src="mcard/images/blog/blog1.jpg" alt="" /></a>
</div>
<div class="content-box">
<div class="i_title">
<div class="icon"><strong>{{ $post->published_at->format('d') }}</strong> {{ $post->published_at->format('M') }}</div>
</div>
<div class="category_bts">
@foreach($post->tags as $tag)
<a href="#" class="category">#{{ $tag->name }}</a>
@endforeach
</div>
<a href="blog-page.html" class="name">{{ $post->title }}</a>
<p>
{{ $post->excerpt }}
</p>
<a href="blog-page.html" class="btn btn_animated"><span class="circle">Leer más</span></a>
</div>
</div>
</div>
</div>
@endforeach
</div>
The problem is that for each row (row) there should be 2 post and if I place the foreach where I am placing it for each row I would repeat the same post 2 times.