按行分开数据项

时间:2019-04-03 00:33:35

标签: laravel bootstrap-4 frontend

我总共有4个使用Laravel的表单数据库,我想将其分开      每行将有2个项目,意味着顶部2个和底部2个

这是我的代码:

@extends('layouts/app') 
@section('content')
    <h1>Clubs & Societies</h1>
    <p>This is Clubs And Societies</p>
    <h2>Categories</h2>
    @if (count($categories)>0)
        @foreach ($categories as $category)
            <div class="container col-md-3">
                <div class="card card-body bg-light row-fluid" style="display:flex">
                    {{$category->category_name}}
                </div>
            </div>
        @endforeach

    @else
        <p>No Category Found</p>
    @endif

@endsection

1 个答案:

答案 0 :(得分:2)

您可以使用chunk()方法将$categories拆分为每个集合有02个项目的集合。

https://laravel.com/docs/5.8/collections#method-chunk

$categories = $categories->chunk(2);

现在,您可以遍历顶部的$categories->first()和底部的$categories->last()