1
下....Laravel集合 - 結果訂購
控制器:
public function treatmentsList()
{
$treatments = Treatment::with('category')->where('status', 1)->orderBy('medical_name')->get();
$groups = $treatments->groupBy('category.name');
return view('pages.treatments.list', compact('groups'));
}
查看:
<ul>
@foreach($groups as $category_name => $treatments)
<li>
<h4>{{ $category_name }}</h4>
<ul>
@foreach($treatments as $treatment)
<li>
<h5>{{ $treatment->medical_name }}</h5>
</li>
@endforeach
</ul>
</li>
@endforeach
</ul>
給我...
的治療方法中的分組類別和按字母順序,但類別不按字母順序。
- Category B
Treatment A
Treatment B
Treatment C
- Category A
Treatment A
Treatment B
- Category C
Treatment A
Treatment B
Treatment C
Treatment D
我怎樣才能得到按字母順序排列的治療和類別?
您是否在groupBy函數調用後嘗試過使用' - > orderBy('category.name')'? –
在groupBy給我之後添加' - > orderBy('category.name')':方法orderBy不存在。 – TickedTask