首页 文章

多个@foreach的观点 - Laravel

提问于
浏览
0

我需要在我的刀片模板中输出各种 @foreach 循环,但它们都在我的数据库中定位同一个表,只是获取不同的字段 . 举个例子,我在主布局中使用@yield,在 Headers /描述标签等视图中使用@section:

@section('title')

@foreach($store_listings as $fetch)
Website stores - {{ $fetch->city }}, {{ $fetch->country }}
@endforeach

@stop

@section('description')

@foreach($store_listings as $fetch)
List of Stores in {{ $fetch->city }}, {{ $fetch->country }}.
@endforeach

@stop

然后在layout.main中:

<title>@yield('title')</title>

<meta name="description" content="@yield('description')" />

重复的@foreach循环存在于其他页面上我的视图的其他部分 . 我试着通过DRY方法工作 . 当我想做的就是调用一些变量时,这里有什么建议来保存我进入多个foreach循环?我也不想让我的控制器来处理这项业务 .

谢谢

1 回答

  • 1

    在这种情况下你可以做的一件事是它使用部分,让我们说 views/_partials/storeList.blade.php

    @foreach($store_listings as $fetch)
       {{$title}} {{ $fetch->city }}, {{ $fetch->country }}
    @endforeach
    

    然后在主视图中调用它:

    @include('_partials.storeList', array('title' => 'Website stores -'))
    
    @include('_partials.storeList', array('title' => 'List of Stores in'))
    

相关问题