[英]Laravel show products with child categories
我有代碼的MainController:
public function index()
{
$products = Product::with('category')->paginate(15);
return view('main', compact('products'));
}
在刀片中:
<table class="table">
<tr>
<th class="th-1">Name</h2></th>
<th class="th-2">Count <!--<span><i class="fas fa-chevron-up"></i>--></span></th>
<th class="th-3">Price <!--<span><i class="fas fa-chevron-up"></i>--></span></th>
<th class="th-4">Sales <!--<span><i class="fas fa-chevron-up"></i>--></span></th>
<th class="th-5"></th>
</tr>
{{-- {{ dd($products->groupBy('category_id')) }} --}}
@include('products')
</table>
<div class="pagination mt-3">
{{ $products->links() }}
</div>
在產品刀片中:
@forelse($products->groupBy('category.title') as $title => $prods)
{{-- @forelse($products as $product) --}}
<tr class="category">
<td colspan="5">{{ $title }}</td>
</tr>
@foreach($prods as $product)
<tr>
<td>
<div class="icon float-left mr-2">
<span class="{{ $product->category->slug ?? '' }}"><i class="{{ $product->category->icon['font'] ?? 'far fa-file-alt' }}" @isset($product->category->icon['color']) style="color: {{ $product->category->icon['color'] }}" @endisset></i></span>
</div>
<p class="mb-0" data-toggle="tooltip" data-placement="top" title="{{ $product->description }}">
{!! $product->title !!}
</p>
</td>
<td>
{{ $product->product_count }}
</td>
<td>
@if($product->discount)
<del class="text-danger">{{ $product->oldPrice }} $.</del>
{{ $product->price }} $. <span class="text-muted">/ 1 cnt.</span>
@else
{{ $product->price }} $. <span class="text-muted">/ 1 cnt.</span>
@endif
</td>
<td>
{{ $product->sales }}
</td>
<td class="text-right">
<a href="{{ route('showproduct', $product) }}" class="btn btn-success btn-sm">
<i class="fas fa-arrow-right"></i>
Readmore
</a>
</td>
</tr>
@endforeach
@empty
<tr>
<td colspan="5">
<p>-</p>
</td>
</tr>
@endforelse
在模型類別中,我有children
, parent
:
public function children()
{
return $this->hasMany(self::class, 'parent_id');
}
public function parent()
{
return $this->belongsTo(self::class, 'parent_id');
}
public function products()
{
return $this->hasMany(Product::class);
}
現在,我在頁面上獲得產品:
類別
1.產品1
2.產品2
3.產品3
另一類
1.產品1
2.產品2
3.產品3
我如何添加孩子並獲得結果?
類別
1.產品
2.產品
----- 子類別
----- 1.產品
----- 2.產品
另一類
1.產品
2.產品
----- 子類別
----- 1.產品
----- 2.產品
3.產品
UPDATE
現在我得到了重復:
類別
1.產品1
2.產品2
3.產品3
另一類
1.產品1
2.產品2
3.產品3
我如何添加孩子並獲得結果?
類別
1.產品
2.產品
----- 子類別
----- 1.產品
----- 2.產品
另一類
1.產品
2.產品
3.產品
類別兒童
1.產品
2.產品
您可以通過遞歸實現此行為。 主要思想是在父產品具有子產品時將產品稱為部分產品。 首先要做的是將所有categories
發送到視圖,而不是products
。 因此,在Controller中:
public function index()
{
$categories = Category::paginate(15);
return view('main', compact('categories'));
}
然后,在視圖中,循環遞歸類別。 這是主要思想:
Products.blade:
//Code...
<h1>List</h1>
@foreach ($categories as $category)
@include('partial', ['category' => $category])
@endforeach
//Code...
然后,您可以創建一個名為partial.blade
的文件:
{{ $category->title }}
@foreach ($category->products as $product)
{!! $product->title !!}
//Other attributes
@endforeach
@if ($category->children()->count() > 0)
@foreach ($category->children as $child)
@include('partial', ['category' => $child])
@endforeach
@endif
主要思想是在特定類別有子級時調用partial.blade。 因此,您再次為每個孩子調用它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.