doufu9145 2016-11-18 14:08 采纳率: 0%
浏览 47
已采纳

OrderBy与Laravel 5.1中的链接数据库表

I have a table with products, one with categories and one linking the products to multiple categories. All with correct foreign keys linking them together. When the user is in a category, the products within this category are selected using the ProductToCategory model.

But what I would like is to find an easy way, without looping through the output of linked products to this category to sort the products by name.

My database layout:

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

In my ProductCategory Model I have:

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

In my ProductToCategory model I have:

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

In the controller I have:

$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);
    }

}

This gives me with $linked_product->Product the product info I need.

But using the Eloquent approach in the models I want to order the products by name before giving me the query result.

How do I go about that?

  • 写回答

1条回答 默认 最新

  • douchun6221 2016-11-18 14:36
    关注

    It seems that you have a many-to-many relation. You do not need a ProductToCategory-Model.

    Instead you need this relation in your ProductCategory-Model:

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

    And one in your Product-Model:

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

    You can also specify the name of the pivot-table (the product_to_category-table) and the column names:

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

    With this you're able to query it as follows:

    $linked_products = $category->products()->orderBy('name')->get();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?