我试图在我的循环中通过'alpha'做一个简单的组,用于词汇表页面但不确定如何实现这一点。我的数据是
alpha glossary_title
Apple B Banana另一个Apple!
GlossaryController.php
public function index() {
$glossary = Glossary::groupBy('alpha')->orderBy('glossary_title', 'asc')->get();
return View::make('glossary')->with('glossary', $glossary);
}
迁移
Schema::create('glossary', function(Blueprint $table)
{
// columns
$table->increments('id');
$table->string('glossary_title');
$table->text('glossary_desc')->nullable();
$table->char('alpha', 1);
$table->timestamps();
});
glossary.blade.php
@foreach($glossary as $glossary)
<li>{{ $glossary->glossary_title}} <br /> {{ $glossary->glossary_desc}}</li>
@endforeach
答案 0 :(得分:0)
这将返回按$glossary->alpha
分组的新收藏:
$glossary = Glossary::orderBy('glossary_title', 'asc')->get();
$glossary = $glossary->groupBy('alpha');
// same as this:
$glossary = $glossary->groupBy(function ($entry) {
return $entry->alpha;
});
您的视图中需要2个循环,首先是组,第二个是每组中的条目。