简体   繁体   English

Laravel显示带有子类别的产品

[英]Laravel show products with child categories

I have MainController with code: 我有代码的MainController:

public function index()
{
    $products = Product::with('category')->paginate(15);

    return view('main', compact('products'));
}

In blade: 在刀片中:

                       <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>

In products blade: 在产品刀片中:

@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

In model Category I have relations children , and parent : 在模型类别中,我有childrenparent

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);
}

Now I get products on page: 现在,我在页面上获得产品:

Category 类别
1. Product 1 1.产品1
2. Product 2 2.产品2
3. Product 3 3.产品3

Another Category 另一类
1. Product 1 1.产品1
2. Product 2 2.产品2
3. Product 3 3.产品3

How I can add child and get the result? 我如何添加孩子并获得结果?

Category 类别
1. Product 1.产品
2. Product 2.产品
----- Category Child ----- 子类别
----- 1. Product ----- 1.产品
----- 2. Product ----- 2.产品

Another Category 另一类
1. Product 1.产品
2. Product 2.产品
----- Category Child ----- 子类别
----- 1. Product ----- 1.产品
----- 2. Product ----- 2.产品
3. Product 3.产品

UPDATE UPDATE

Now I get duplicates: 现在我得到了重复:

Category 类别
1. Product 1 1.产品1
2. Product 2 2.产品2
3. Product 3 3.产品3

Another Category 另一类
1. Product 1 1.产品1
2. Product 2 2.产品2
3. Product 3 3.产品3

How I can add child and get the result? 我如何添加孩子并获得结果?

Category 类别
1. Product 1.产品
2. Product 2.产品
----- Category Child ----- 子类别
----- 1. Product ----- 1.产品
----- 2. Product ----- 2.产品

Another Category 另一类
1. Product 1.产品
2. Product 2.产品
3. Product 3.产品

Category Child 类别儿童
1. Product 1.产品
2. Product 2.产品

You can achieve this behavior with recursion. 您可以通过递归实现此行为。 The main idea is to call a products partial when the parent product has children. 主要思想是在父产品具有子产品时将产品称为部分产品。 The first thing to do is to send all categories to the view, not the products . 首先要做的是将所有categories发送到视图,而不是products So, in Controller: 因此,在Controller中:

public function index()
{
    $categories = Category::paginate(15);

    return view('main', compact('categories'));
}

And then, in the view, you loop the categories in recursion. 然后,在视图中,循环递归类别。 This is the main idea: 这是主要思想:

Products.blade: Products.blade:

//Code...
<h1>List</h1>
@foreach ($categories as $category)
    @include('partial', ['category' => $category])
@endforeach
//Code...

And then you can create a file called partial.blade : 然后,您可以创建一个名为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

The main idea is to call partial.blade when a specific category has children. 主要思想是在特定类别有子级时调用partial.blade。 So, you call it for every child again. 因此,您再次为每个孩子调用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM