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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀