【问题标题】:Laravel orderBy with deep relationshipLaravel orderBy 关系很深
【发布时间】:2018-06-06 15:23:17
【问题描述】:

谁能帮我解决我的订购问题。

我有 4 张桌子:

posts 
[id]

post_values 
[id, post_id, value_id, value_text]

post_category_field_values 
[id]

post_category_field_value_translations 
[id, value_id, 'locale', 'name']

我需要按翻译值名称排序所有帖子。

【问题讨论】:

  • 你没有在 post_category_feild_value_transaltions 中存储 post_id?
  • 不,我有带字段的类别。当我创建一个帖子时,我应该为“post_values”表中的类别中的每个字段定义值。
  • 现在我正在尝试进行排序和排序功能。稍后我想添加一个过滤功能。
  • 所以我想订购这个关系: $posts = Post::with('value.value.translation')->orderBy('value.value.translation.name', 'ASC') ->get();

标签: php mysql laravel eloquent


【解决方案1】:

也许 Jarek 的回答会对你有所帮助

    $products = Shop\Product::join('shop_products_options as po', 'po.product_id', '=', 'products.id')
   ->orderBy('po.pinned', 'desc')
   ->select('products.*')       // just to avoid fetching anything from joined table
   ->with('options')         // if you need options data anyway
   ->paginate(5);

Laravel Eloquent sort by relation table column

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-08-11
    • 1970-01-01
    • 2020-03-25
    • 2014-08-04
    • 1970-01-01
    • 1970-01-01
    • 2017-09-22
    • 1970-01-01
    相关资源
    最近更新 更多