【问题标题】:OrderBy with linked database tables in Laravel 5.1OrderBy 与 Laravel 5.1 中的链接数据库表
【发布时间】:2017-04-02 10:11:34
【问题描述】:

我有一个包含产品的表格,一个包含类别,一个将产品链接到多个类别。所有这些都具有将它们链接在一起的正确外键。当用户在一个类别中时,使用ProductToCategory模型选择该类别中的产品。

但我想要找到一种简单的方法,而不是将链接产品的输出循环到该类别以按名称对产品进行排序。

我的数据库布局:

products
    id: 1; name: testing 123
    id: 2; name: testing 345
    id: 3; name: testing 567

product_categories
    id: 1; name: cat one
    id: 2; name: cat two
    id: 3; name: cat three

product_to_categories
    id: 1; product_id: 1; product_category_id: 1
    id: 2; product_id: 1; product_category_id: 2
    id: 3; product_id: 2; product_category_id: 1
    id: 4; product_id: 3; product_category_id: 1
    id: 5; product_id: 3; product_category_id: 2
    id: 6; product_id: 3; product_category_id: 3

在我的 ProductCategory 模型中,我有:

public function ProductToCategory() {
    return $this->hasMany('App\Models\ProductToCategory');
}

在我的 ProductToCategory 模型中,我有:

public function Product() {
    return $this->belongsTo('App\Models\Product', 'product_id');
}

在我的控制器中:

$linked_products = $category->ProductToCategory()->get();
foreach($linked_products as $linked_product) {
    if($linked_product->Product->is_active == 1) {
        $array[$linked_product->product_id] = $linked_product->Product);
    }

}

这给了我$linked_product->Product 我需要的产品信息。

但是在模型中使用 Eloquent 方法,我想在给我查询结果之前按名称订购产品。

我该怎么做?

【问题讨论】:

  • 什么是 ProductToCategory?为什么不将多对多与 belongsToMany 一起使用?
  • ProductToCategory 是一个表格,其中一个产品可以链接到多个类别。因此,例如 product_id 1 在数据库中为 product_id 1; category_id 1;product_id 1; category_id 2;。因此,我使用 category_id 进行查询以获取该类别中的所有产品。如果每个产品都链接到一个类别会更简单

标签: php mysql laravel laravel-5


【解决方案1】:

您似乎有一个many-to-many relation。您不需要 ProductToCategory-Model。

相反,您需要在 ProductCategory-Model 中使用这种关系:

public function products()
{
    return $this->belongsToMany('App\Models\Product');
}

还有一个在您的产品模型中:

public function categories()
{
    return $this->belongsToMany('App\Models\ProductCategory');
}

您还可以指定数据透视表的名称(product_to_category-table)和列名:

public function categories()
{
    $this->belongsToMany('App\Models\ProductCategory', 'table_name', 'product_id ', 'product_category_id ');
}

有了这个,你可以按如下方式查询它:

$linked_products = $category->products()->orderBy('name')->get();

【讨论】:

  • 但是哪里注意到product id 1属于category id 1和category id 2呢?
  • @AlvinBakker 看看我更新的答案。 Laravel 根据您的模型名称猜测名称。但是,如果列或表的名称不同,您可以明确指定它们。
  • 我已经用数据库布局调整了这个问题,向您展示我的意思为什么我需要product_to_categories 表,因为一个产品可以分为 3 个类别。您调整后的答案是类别和产品链接在一起,只是命名不同。但事实并非如此,因为两者之间有一个。希望在我的问题中添加数据库布局可以使这一点更加清晰
  • @AlvinBakker 感谢数据库架构!这是一个多对多数据透视表模式。您不需要 ProductToCategory-Model 但您当然需要表格。相信我,按照我的回答中所述尝试。它将按您的需要工作! Laravel 知道多对多关系。它非常适合这种枢轴模式。你可以在这里阅读更多信息:laravel.com/docs/5.3/eloquent-relationships#many-to-many
  • 好的,我得到你的答案; Laravel 会自动生成这些关系。但是当我用你的例子调整我的模型时,我得到这个错误:Base table or view not found: 1146 Table 'spincms.product_product_category' doesn't exist (SQL: select products.*, product_product_category.product_category_id` as pivot_product_category_id, product_product_category.product_id as pivot_product_id from products inner join product_product_category on products.id = product_product_category.product_id where product_product_category.product_category_id = 1 order by name asc)`
猜你喜欢
  • 2015-10-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-11
  • 2016-11-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多