代码之家  ›  专栏  ›  技术社区  ›  Szabolcs

@包含在@foreach中,继续使用集合的第一个元素

  •  0
  • Szabolcs  · 技术社区  · 6 年前

    在视图中,我试图@包含一个组件,如:

    @foreach($products as $product)
        <div class="col m4">
            {{$product -> model}}
            @include('components.thumb-product', ['model' => $product -> model])
        </div>
    @endforeach
    

    组件内容。拇指产品:

    @extends('layouts.cards.thumb-product-a')
    @section('model')
        {{$model}}
        {{$product -> model}}
    @endsection
    

    我在$products中有5个不同的元素,@include(“components.thumb product”)每次迭代都会输出第一个元素。

    但是在@foreach中,$product->model(在@include之前)输出了我所期望的:

    DBTPI0PGC2、OWQE、TDH、8WFXWRGL、C4M

    @include('components.thumb product'),$model&$product->model输出:

    DBTPI0PC2、DBTPI0PC2、DBTPI0PC2、DBTPI0PC2、DBTPI0PC2

    DBTPI0PC2、DBTPI0PC2、DBTPI0PC2、DBTPI0PC2、DBTPI0PC2

    我不知道为什么会这样,我应该使用@include以外的其他东西吗? 请给我指路。

    1 回复  |  直到 6 年前
        1
  •  1
  •   Alex Nikishkin    6 年前

    尝试在components.thumb-product中使用@overwrite而不是@endsection

    @section('model')
        {{$model}}
        {{$product -> model}}
    @overwrite
    @extends('layouts.cards.thumb-product-a')
    

    我也有同样的问题。