Php 叶片问题中的Laravel多重foreach
我有一些片段。细分市场中有一些类别。每一部分由3个类别组成,中间部分为主要部分。如何在刀片上显示它们 我这样写代码Php 叶片问题中的Laravel多重foreach,php,laravel,web,foreach,Php,Laravel,Web,Foreach,我有一些片段。细分市场中有一些类别。每一部分由3个类别组成,中间部分为主要部分。如何在刀片上显示它们 我这样写代码 $seg = DB::table('segments')->get(); $categories = DB::table('categories') ->join('category_translations', 'category_translations.category_id', 'categories.id')
$seg = DB::table('segments')->get();
$categories = DB::table('categories')
->join('category_translations', 'category_translations.category_id', 'categories.id')
->select('categories.id', 'category_translations.category_name')
->where('language_id', $lan->id)
->get();
$seg_cat = DB::table('segment_categories')->get();
@foreach($seg as $segment)
<section class="news-block">
<div class="container">
<div class="row">
@foreach($seg_cat as $segment_categories)
@if($segment->id == $segment_categories->segment_id)
@foreach($categories as $cat)
@if($cat->id == $segment_categories->category_id)
<div class="col-lg-3">
</div>
<div class="col-lg-6">
</div>
</div>
<div class="col-lg-3">
</div>
@endif
@endforeach
@endif
@endforeach
$seg=DB::table('segments')->get();
$categories=DB::表('categories')
->join('category\u translations'、'category\u translations.category\u id'、'categories.id')
->选择('categories.id'、'categories\u translations.categories\u name')
->其中('language_id',$lan->id)
->get();
$seg_cat=DB::table('segment_categories')->get();
@foreach($seg作为$segment)
@foreach($seg_cat作为$segment_类别)
@如果($segment->id==$segment\u categories->segment\u id)
@foreach($类别为$cat)
@如果($cat->id==$segment\u categories->categories\u id)
@恩迪夫
@endforeach
@恩迪夫
@endforeach
这给了我这样的感觉
这种情况取决于您如何处理和显示刀片文件中需要处理的数据
{
"data": [
{
"segmentName1": "Foo",
"categories": [
{
"title": "Foo"
},
{
"title": "Boo"
},
{
"title": "Roo"
}
]
},
{
"segmentName2": "Boo",
"categories": [
{
"title": "Foo"
},
{
"title": "Boo"
},
{
"title": "Roo"
}
]
},
{
"segmentName3": "FooBoo",
"categories": [
{
"title": "Foo"
},
{
"title": "Boo"
},
{
"title": "Roo"
}
]
}
]
}
最好的选择是在将数据传递到前端之前在控制器中构造代码和foreach循环,以使刀片模板中的逻辑更简单
下面是一个Json
数组中的示例,它可以处理两个foreach循环,并且不会使刀片文件中的循环变得更复杂
{
"data": [
{
"segmentName1": "Foo",
"categories": [
{
"title": "Foo"
},
{
"title": "Boo"
},
{
"title": "Roo"
}
]
},
{
"segmentName2": "Boo",
"categories": [
{
"title": "Foo"
},
{
"title": "Boo"
},
{
"title": "Roo"
}
]
},
{
"segmentName3": "FooBoo",
"categories": [
{
"title": "Foo"
},
{
"title": "Boo"
},
{
"title": "Roo"
}
]
}
]
}