1

I'm trying to find a way to do the following using eloquent:

I have products with brands related by a many to many relationship. Brands are nested using nested sets (https://github.com/lazychaser/laravel-nestedset) and are only one level deep (brand and sub brand), so products will have at most 2 brands (a parent and a child), but could also only have a parent since some brands don't have a child brand

I need to order the products by parent brand (ie the highest level brand assigned to a product) title, how if at all, can I achieve this using eloquent?

$products_builder->with(['brands' => function($query)
    {
        $query->orderBy('title', 'desc');
    }
]);

Won't work here.

EDIT: Just adding some more info that might make the problem simpler to understand. I need to sort products by root brand. Each product could have multiple brands but will only ever have one root brand. The root brand would have parent_id = null

  • Does this answer your question? [Eloquent eager load Order by](https://stackoverflow.com/questions/18861186/eloquent-eager-load-order-by) – Yves Kipondo Jan 09 '20 at 14:10

0 Answers0