首页 > 解决方案 > Laravel 5.8 - 使用 Laravel-nestedset 查询属于当前类别的产品

问题描述

我正在使用 Laravel 5.8 和Laravel-nestedset作为我的类别模型,并且我有一个产品模型。

我成功地创建了嵌套类别并正确显示它们,但是当我进入一个有产品的类别时,我想显示属于当前类别的产品——下面的代码显示产品,但不在他们所属的类别。这是我想象的非常标准的东西,但我似乎无法弄清楚。

分类.php

<?php

namespace App;

use Spatie\Sluggable\HasSlug;
use Spatie\Sluggable\SlugOptions;
use Illuminate\Database\Eloquent\Model;
use Kalnoy\Nestedset\NodeTrait;

class Category extends Model
{
    use \Spatie\Tags\HasTags;
    use HasSlug;
    use NodeTrait;

    public function getSlugOptions() : SlugOptions
    {
        return SlugOptions::create()
            ->generateSlugsFrom('name')
            ->saveSlugsTo('slug');
    }

    public function getRouteKeyName()
    {
        return 'slug';
    }
}

产品.php

<?php

namespace App;

use Spatie\Sluggable\HasSlug;
use Spatie\Sluggable\SlugOptions;
use Illuminate\Database\Eloquent\Model;

class Product extends Model
{
    use \Spatie\Tags\HasTags;
    use HasSlug;

    protected $dates = [
        'published_on',
    ];

    public function getSlugOptions() : SlugOptions
    {
        return SlugOptions::create()
            ->generateSlugsFrom('title')
            ->saveSlugsTo('slug');
    }

    public function getRouteKeyName()
    {
        return 'slug';
    }

    public function category()
    {
        return $this->belongsTo(Category::class);
    }
}

类别控制器.php

<?php

namespace App\Http\Controllers;

use App\Category;
use App\Product;

class CategoryController extends Controller
{
    public function index()

    {
        $categories = Category::get()->toTree();;

        return view('categories.index', compact('categories'));
    }

    public function show(Category $category)
    {
        $products = Product::with('category')->whereIn('category_id', $category)->get();
        return view('categories.show', compact('category', 'products'));
    }

}

show.blade.php(这是类别展示模板,也是我希望产品展示的地方)

...
@if (count($category['children']) > 0)
    @foreach($category['children']->chunk(3) as $chunk)
        <div class="row">
            @foreach($chunk as $category)
                <div class="col-md-4" style="margin-bottom: 2rem">
                    <div class="feature-box center media-box fbox-bg">
                        <div class="fbox-media">
                            <a href="/{{$category->slug}}">
                                <img class="image_fade"
                                     src="https://*****.s3-us-west-1.amazonaws.com/{{ $category->photo }}"
                                     alt="Featured Box Image"
                                     style="opacity: 1;"></a>
                        </div>
                        <div class="fbox-desc">
                            <h3>{{$category->name}}</h3>
                            <span><a href="{{$category->slug}}">Learn More</a></span>
                        </div>
                    </div>
                </div>
            @endforeach
        </div>
    @endforeach
@endif
@if(count($products) > 0)
    @foreach($products as $product)
        {{$product->title}}
    @endforeach
@endif
...

标签: phplaravellaravel-5eloquent

解决方案


products()在 Category 模型中定义关系

public function products()
{
    return $this->hasMany(Product::class);
}

您可以从实例化类别中访问关系,如下所示:

@if (count($category['children']) > 0)
    @foreach($category['children']->chunk(3) as $chunk)
        <div class="row">
            @foreach($chunk as $category)
                // ...
                @foreach($category->products as $product)
                @endforeach
                // ...
            @endforeach
        </div>
    @endforeach
@endif

推荐阅读