php - 将一个对象切成 3 block 并在 Blade 中查看它们 - Laravel - chunk 方法

标签 php html laravel

我有一个包含多个数据的对象:

    $products = Products::whereIn('id', $input['product_id'])->where('customer_id', Auth::user()->customers_id)->get();

$products 的 dd 输出如下所示:

Collection {#287 ▼
  #items: array:12 [▼
    0 => Products {#288 ▶}
    1 => Products {#289 ▶}
    2 => Products {#290 ▶}
    3 => Products {#291 ▶}
    4 => Products {#292 ▶}
    5 => Products {#293 ▶}
    6 => Products {#294 ▶}
    7 => Products {#295 ▶}
    8 => Products {#296 ▶}
    9 => Products {#297 ▶}
    10 => Products {#298 ▶}
    11 => Products {#299 ▶}
  ]
}

我将对象返回到我的 Blade 并尝试在 3 列上输出数据,如下所示:

    @foreach($products->chunk(3) as $chunk)
        <div class="row" style="width: 100%">
            @foreach($chunk as $product)
                <div style="width: 33%">{{ $product->name }}<br></div>
            @endforeach
        </div>
    @endforeach

但这只是给了我它们之间的数据

喜欢:

A
B
C
D
...

但我需要它在 3 列上

喜欢:

A  E  I
B  F  J
C  G  K
D  H  L

感谢您的帮助!抱歉我的英语不好..

我在以下位置找到了 chunk 方法:

https://laravel.com/docs/5.1/collections

最佳答案

尝试为div's添加float:left;,它应该会产生您想要的结果:

@foreach($products as $chunk)
    <div class="row" style="width: 100%">
        @foreach($chunk as $product)
            <div style="width: 33%; float:left">{{ $product->name }}<br></div>
        @endforeach
    </div>
@endforeach

希望有帮助 =)

关于php - 将一个对象切成 3 block 并在 Blade 中查看它们 - Laravel - chunk 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39160918/

相关文章:

PHP 重定向 header

php - 使用 Magento 时,接近开发环境的好方法是什么?

需要 1 个像素边框的 HTML 表格

javascript - 如何使用 laravel 保存裁剪后的头像

mysql - 当我尝试通过帖子访问类别时,类别关系出现错误

php - 未通过 gmail 收到 laravel 5 邮件

php - 将Google Analytics(分析)标签插入任意页面的最简单方法?

html - 特定 div 中所有媒体屏幕的中心元素

html - <table> 可以同时具有 border-radius 和 border 属性吗?

Laravel echo - 无法通过私有(private) channel 进行身份验证