2014-07-09 152 views
0

我想通過循環中的'alpha'做一個簡單的組,用於詞彙表頁面,但不知道如何實現這一點。我的數據是laravel 4組集合/結果

阿爾法glossary_title

一個蘋果乙香蕉蘋果的另一!

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

這將返回$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個循環,然後首先針對組,其次針對每個組中的條目。