Laravel 5:按类别对产品进行排序,

迈克·爱丁格

我没有laravel的丰富经验,但是我正在尝试按类别对我的产品进行分类...我已经可以对所有产品进行分类,但不能按类别进行分类。

你们能帮帮我吗?

模型:

class Product extends Model {


protected $table = 'products';


public function getCategoriesListAttribute(){
    return $this->categories()->lists('id');
}


public function categories(){
    return $this->belongsToMany('App\Modules\Webshop\Models\ProductCategory', 'category_product', 'product_id', 'category_id')->withTimestamps();
}}

产品表:

    Schema::create('products', function(Blueprint $table)
    Schema::create('category_product', function(Blueprint $table)
    Schema::create('product_categories', function(Blueprint $table)
    Schema::create('product_tag', function(Blueprint $table)

控制器:

class ProductsController extends Controller {

    public function __construct()
    {
        //$this->middleware('auth');
    }

    public function index()
    {
        $viewmodel = array(
            "categories"=> ProductCategory::where('visible', '1')->get(),
            "products" => Product::has('categories')->get(),
            "page"=>Page::where('href','=', '/')->first(),
        );
        return view('Webshop::frontend.view.products.index', $viewmodel);
    }

}

index()仅显示具有任何类别的产品。

如果你们需要更多信息,请随时询问:)

编辑:

我正在我的控制器中尝试此操作。

 public function index()
{
    $viewmodel = array(
        "products" => Product::with('categories')->where('category_id','23'),
    );
    return view('Webshop::frontend.view.products.index', $viewmodel);
}

和这个:

  $viewmodel = array(
        "products" => Product::leftJoin('category_product', 'category_product.product_id', '=', 'products.id')
                        ->leftJoin('product_categories', 'product_categories.id', '=', 'category_product.category_id')
                        ->where('category_id', 23)
                        ->first(['products.*']),
    );
    return view('Webshop::frontend.view.products.index', $viewmodel);

我有一个ID为23的类别。

迈克·爱丁格

解决它!

我将此添加到我的模型中

 public function scopeGetCategorieSlug($query, $category_slug)
{
    return $query->leftJoin('category_product', 'category_product.product_id', '=', 'products.id')
                ->leftJoin('product_categories', 'product_categories.id', '=', 'category_product.category_id')
                ->where('product_categories.slug', $category_slug)
                ->get(['products.*']);
}

这给我的控制器

public function show($category_slug)
{
    $viewmodel = array(
        "categories"=> ProductCategory::where('visible', '1')->get(),
        "products" => Product::getCategorieSlug($category_slug),
        "page"=>Page::where('href','=', '/')->first(),
    );
    return view('Webshop::frontend.view.products.index', $viewmodel);
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何根据特定类别laravel-7对产品进行分页

供应商/商店分组产品按类别-laravel

Laravel排序按分页

按类别对产品排序(在父类别视图中)

Laravel按日期对集合进行排序

按添加日期对文件进行排序Laravel PHP

Laravel 5:是否存在不区分大小写的方式来按属性对集合进行排序?

如何在商店页面上按类别对woocommerce产品进行分类

根据产品枚举中的类别对项目进行分组-Java

如何在Django中按类别对主题进行排序?

如何按类别对集合目录进行排序?

按col1的行数和类别对DataFrame进行排序

WooCommerce按类别对订单项进行排序

按类别对WordPress帖子进行分组

在Django中按类别对项目进行排序

每个日期按类别对数据进行排序

按类别和无限子类别对对象数组进行排序的最佳方法

laravel最新文章按类别排序

首先按根类别对类别数组进行排序

Laravel Eloquent 按元素对组进行排序

如何根据类别对产品进行排序

Laravel - 按 created_at 进行分页排序

按类别对数值进行分组

Laravel Eloquent - 按数组顺序按列对集合进行排序

Woocommerce 使用挂钩或过滤器按类别对产品进行排序

在 Java 或 C# 中按汽车公司产品类别对列表进行排序时需要帮助

Laravel:按关系排序(产品按第一次订购日期排序)

按类别对购物车中的产品进行排序,并将此排序方法应用于 WooCommerce 中的订单页面和电子邮件通知

如何按特定类别对堆积条形图进行排序?