如何在laravel叶片中获得单个类别及其子类别

时间:2019-05-18 05:39:31

标签: laravel

我有一个代码,可以从表中获取所有类别及其子类别,但是我想从laravel刀片导航文件中显示的类别表中获取单个类别及其子类别。

例如,我在表中有鞋子和衬衫的类别及其子类别,我只想显示前端显示的衬衫类别及其子类别。请帮助我实现这一目标。

我从表格中获取所有类别及其子类别

路线:

//here i have added route url for link and well to display categories 
Route::get('/products/{url}', 'ProductController@products');

控制器方法:

//this is function/method
public function products($url = null)
    {
            $countCategory = Category::where(['url'=>$url, 'status'=>1])->count();
                if($countCategory==0){
                    abort(404);
                }
        //Get All Categories and Sub Categories
       $categories = Category::with('categories')->where(['parent_id' => 0])->get();

       $categoryDetails = Category::where(['url' => $url])->first();
            if ($categoryDetails->parent_id==0) {
                //if url is main category url
                $sub_categories = Category::where(['parent_id'=>$categoryDetails->id])->get();
                    foreach ($sub_categories as  $sub_cat) {
                        $cat_ids[] = $sub_cat->id;
                    }
                    // echo $cat_ids; die;
                    $productsAll = Product::whereIn('category_id',$cat_ids)->where('status', 1)->get();
            } else {
                $productsAll = Product::where(['category_id' => $categoryDetails->id])->where('status', 1)->get();
            }
       return view('products.listing')->with(compact('categories', 'categoryDetails', 'productsAll'));
    }

类别模型:

 public function categories()
    {
        return $this->hasMany('App\Category', 'parent_id');
    }

裸露(查看)文件:

<div class="side-bar col-md-12">
    <h3 class="agileits-sear-head">Category</h3>
    <div class="panel-group" id="accordian">
        {{-- this is the basic way to show the categores inthe table --}}
        <?php //echo $categories_menu ?>
            @foreach ($categories as $cat) 
            @if($cat->status=="1")
                <div class="panel-heading" >                                   
                    <h4 class="panel-title">
                        <a href="#{{ $cat->id }}" data-toggle="collapse" data-parent="#accordian">
                            <span class="badge pull-right"><i class="fa fa-plus"></i></span>
                                {{ $cat->name }}
                        </a>
                    </h4>
                </div>
                <div id="{{ $cat->id }}" class="panel-collapse collapse">
                    <div class="panel-body" >
                        <ul style="list-style: none; margin-left:30px;">
                            @foreach ($cat->categories as $sub_cat)
                                @if($sub_cat->status=="1")
                                    <li><a  href="{{ asset('/products/'.$sub_cat->url)  }}" style="color:orange;">{{ $sub_cat->name }}</a></li>
                                @endif
                            @endforeach
                        </ul>
                    </div>
                </div>
            @endif    
            @endforeach        
    </div>
</div>

我希望尽快解决该问题 先生,我有一个问题,我不想在边栏中显示我的所有类别,而不是只想显示一个类别,例如鞋类及其子类别,请指导我如何实现此目的。而且我还希望针对不同类别使用不同类别的侧边栏,

示例, 假设我们有一个衬衫列表页面,其中所有衬衫,T恤以及更多与衬衫类别相关的衬衫。 而且我只希望在边栏中显示“衬衫”及其子类别。

如果我有鞋子类别,并且只想在侧边栏中显示鞋子类别及其子类别。

请指导我如何实现这一目标,

1 个答案:

答案 0 :(得分:0)

这很简单,有很多关系,让我告​​诉你。

<?php

namespace App\models;
use App\pivotes\Catagory;

use Illuminate\Database\Eloquent\Model;

class Catagory extends Model
{
    public function children()
    {
        return $this->hasMany(SELF::class, 'parent_id');
    }

    public function parentCategory()
    {
        return $this->hasMany(SELF::class, 'id', 'parent_id');
    }
}

迁移文件为

Schema::create('categories', function (Blueprint $table) {
            $table->increments('id');
            $table->string('category');
            $table->unsignedInteger('parent_id')->nullable();
            $table->timestamps();

            $table->foreign('parent_id')->references('id')->on('comments');
        });

/categories/{category}

控制器方法将

CategoryController extends Controller {
    public function show(Category $category)
    {
        return view('categories.show', $category);
    }

}

可见

@foreach( $category->children as $subCategory )
    {{ $subcategory->category }}
@endforeach