【问题标题】:how to get single category and its subcategories in laravel blade如何在 laravel Blade 中获取单个类别及其子类别
【发布时间】:2019-10-05 08:51:52
【问题描述】:

我有一个代码,我从表中获取所有类别及其子类别,但我想从 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');
    }

Balde(视图)文件:

<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 恤和更多与衬衫类别相关的衬衫。 我只想在侧边栏中显示衬衫及其子类别。

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

请指导我如何实现这一点,先生,

【问题讨论】:

  • 你试过我的解决方案了吗?

标签: laravel


【解决方案1】:

很简单的hasMany-belongsTo关系,我来给你看看。

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

【讨论】:

    猜你喜欢
    • 2021-12-20
    • 2014-04-25
    • 2018-12-20
    • 2021-12-22
    • 1970-01-01
    • 1970-01-01
    • 2022-01-27
    • 2021-08-28
    • 2015-05-14
    相关资源
    最近更新 更多