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