@foreach in laravel that goes from 2 objects to 2 objects?

4

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.

    
asked by Jesús Henríquez 12.08.2018 в 06:07
source

2 answers

4

It is solved in the following way:

 @foreach($posts->chunk(2) as $post)
<div class="row">
    @foreach($post as $postf)
    <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>{{ $postf->published_at->format('d') }}</strong> {{ $postf->published_at->format('M') }}</div>
                    </div>
                    <div class="category_bts">
                        @foreach($postf->tags as $tag)
                            <a href="#" class="category">#{{ $tag->name }}</a>
                        @endforeach
                    </div>
                    <a href="blog-page.html" class="name">{{ $postf->title }}</a>
                    <p>
                        {{ $postf->excerpt }}
                    </p>
                    <a href="blog-page.html" class="btn btn_animated"><span class="circle">Leer más</span></a>
                </div>
            </div>
        </div>
    @endforeach
</div>
@endforeach

That is, the segment

  

@foreach ($ posts-> chunk (2) as $ post)

What you do is make a split to the arrangement $ posts and place it from 2 to 2 in another arrangement called $ post, that's why then you should place this other segment

  

@foreach ($ post as $ postf)

Where that arrangement that goes from 2 to 2 will go through 1 in 1 to fit the template.

    
answered by 12.08.2018 / 14:53
source
0

And why do not you simply iterate inside of row printing columns? When the columns exceed the limit of 12 units they should be printed below giving the allusion to another row visually, if there are still empty spaces and columns They do not fill properly, it would be your styles problem. For what you are doing your posts should have the same height and if they do not have it, it would be, in my opinion, bad interface design.

Your code should look like this:

<!-- Blog -->
                <div class="section blog-posts" id="blog-section"> 
                    <div class="row">
                    @foreach($posts as $post)
                        <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>
                    @endforeach
                    </div>
                </div>
    
answered by 15.08.2018 в 18:08