0

I have:

  • 'cards' table -id -name

  • 'card_categories' Table

    • id
    • card_id
    • category_id
  • 'categories' Table

    • id
    • name
    • index

I'am Loading The Card then Eager load the Relation, what i would like to do is when doing this :

Card::with('ctrCategories.category').......;

I would like that all loaded category from categories will be sorted by Index just the categories.

I spent the hole day doing everything , but no solution:

I tried this:

$card = Card::findOrFail($id); return $card->with('cardCategories')

   ->with('ctrCategories.category')
   ->with('ctrCategories.arguments')
   ->orderBy('ctrCategories.category.index')->get();

I also tried this approach:

$data = $this->card
->with([
    'roles' => function ($q) {
        $q->with(['tabs' => function ($q) {
            $q->with(['department' => function ($q) {
                $q->with(['panel' => function ($q) {
                    $q->orderBy('position', 'asc');
                }])->orderBy('position', 'asc');
            }])->orderBy('position', 'asc');
        }])->orderBy('position', 'asc');
    }
])

->findOrFail($id);

=====EDIT=====

I writed the SQL query and i got the result i want Now i want to transform it to Laravel Eloquent or DB query:

select cards.id, cards.name,categories.name,categories.id,categories.index from cards inner join card_categories on cards.id = card_categories.card_id inner join categories on categories.id = card_categories.category_id where cards.id = 120 AND cards.support_id= categories.support_id order by categories.index asc

any help ? I can't figure it out after couple of hours of testing

TnCoder
  • 1
  • 1
  • You can pass a closure with the `orderBy` when you are eager loading the relation, see [here](https://stackoverflow.com/a/18862651/9193055). – Remul Jan 10 '19 at 14:05
  • Already tested that and same problem – TnCoder Jan 10 '19 at 14:44
  • try doing this in the Card Model file in the relation function `function cardCategeories(){ return $this->hasMany(cardCategeory::class)->orderBy('desc');}` i tried to do whereIn query in my project & it worked with me , this may work – Islam ElHakmi Jan 10 '19 at 15:10
  • @EslamElhakmey Hi, i want to sort the result of the loaded relation category not cardCategories, cardCategories is the Pivot table, so i did this: public function categories() { return $this->hasManyThrough(Category::class, CardCategory::class); } – TnCoder Jan 10 '19 at 15:20
  • i meant trying to add whatever query you want to the relation, like this `function categories() { return $this->hasManyThrough(Category::class, CardCategory::class)->orderBy('position', 'asc'); }` try doing this, it may work – Islam ElHakmi Jan 10 '19 at 21:33
  • =====EDIT===== I writed the SQL query and i got the result i want Now i want to transform it to Laravel Eloquent or DB query: select cards.id, cards.name,categories.name,categories.id,categories.index from cards inner join card_categories on cards.id = card_categories.card_id inner join categories on categories.id = card_categories.category_id where cards.id = 120 AND cards.support_id= categories.support_id order by categories.index asc – TnCoder Jan 11 '19 at 08:48

0 Answers0