2016-11-16 211 views
0

我有我的Laravel application.But簡單的問題我不能找到it.Please的任何解決辦法看到我的代碼在這裏:如何顯示數據庫中的所有行 - Laravel 5.2?

控制器:

public function index(){ 
    $members = Member::paginate(15); 

    $meal_entry = Member::all(); 
    $breakfast = DB::SELECT("SELECT self_meal_breakfast + guest_meal_breakfast AS breakfast FROM meal_entry"); 
    $lunch = DB::SELECT("SELECT self_meal_lunch + guest_meal_lunch AS lunch FROM meal_entry");   
    $dinner = DB::SELECT("SELECT self_meal_dinner + guest_meal_dinner AS dinner FROM meal_entry"); 
    $total_meal = DB::SELECT("SELECT self_meal_breakfast + self_meal_lunch + self_meal_dinner + guest_meal_breakfast + guest_meal_lunch + guest_meal_dinner AS total FROM meal_entry"); 

    return view('member.mealView', compact('members','data','breakfast','lunch','dinner','meal_entry','total_meal')); 
} 

刀片觀點:

<tbody> 
    <tr> 
     @foreach($meal_entry as $value) 
     <td>{{ $value->created_at }}</td> 
     @endforeach           
     @foreach($breakfast as $value) 
     <td>{{ $value->breakfast }}</td> 
     @endforeach           
     @foreach($lunch as $value) 
     <td>{{ $value->lunch }}</td> 
     @endforeach           
     @foreach($dinner as $value) 
     <td>{{ $value->dinner }}</td> 
     @endforeach           
     @foreach($total_meal as $value) 
     <td>{{ $value->total }}</td> 
     @endforeach           
     @foreach($meal_entry as $value) 
     <td>{{ $value->created_at }}</td> 
     @endforeach 
    </tr> 
</tbody> 

和表:meal_entry

如果有一個row在數據庫中,然後其確定(see)到view.But當有row不止一個那麼它在一個row所有數據(see)節目。如何我可以處理這件事情?我認爲它應該運行另一個循環,但我該怎麼做?提前致謝。

+0

拜託,你能告訴我們該怎麼樣的表看起來像? 謝謝! –

+1

@IdirOuhabMeskine當然... http://prntscr.com/d7whid –

+0

由於數據在一個表中。您應該執行單個查詢。 –

回答

4

所以,如果你正在使用Laravel中,具有最佳功能之一是存取:

https://laravel.com/docs/5.2/eloquent-mutators#accessors-and-mutators

您可以創建新的屬性和模型屬性中使用它們。

對於一些東西是非常有用的。

class Meal extends Model 
{ 
    public function getTotalBreakfastAttribute() 
    { 
     return $this->self_meal_breakfast + $this->guest_meal_breakfast; 
    } 
} 

,然後用此訪問的屬性:

<tbody> 
    @foreach($foods as $value) 
    <tr> 
     <td> {{$value->total_breakfast}} </td> 
     <td> {{$value->created_at}} </td> 
    </tr> 
    @endforeach 
</tbody> 

或者,如果你不想這樣做,而不是使用多個查詢來獲取數據,只需使用一個,因爲所有的字段在同一個表中,然後,您可以使用上面的示例。

享受! :)

+1

是的,這是我尋找的東西。+++ –

0

一個快速補丁將只做一個查詢,然後對視圖進行計算(我知道這不是一個好的做法,在視圖中進行計算,但是我可以看到的最簡單的方法)。

這將是這樣的:

控制器:

public function index(){ 
    $members = Member::paginate(15); 

    return view('member.mealView', compact('members')); 
} 

刀片觀點

<tbody> 
    @foreach($members as $meal_entry) 
    <tr> 
     <td>{{ $meal_entry->created_at }}</td> 

     <td>{{ $breakfast = $meal_entry->self_meal_breakfast + $meal_entry->guest_meal_breakfast }}</td> 

     <td>{{ $lunch = $meal_entry->self_meal_lunch+ $meal_entry->guest_meal_lunch}}</td> 

     <td>{{ $dinner = $meal_entry->self_meal_dinner + $meal_entry->guest_meal_dinner }}</td> 

     <td>{{ $breakfast + $lunch + $dinner }}</td> 

     <td>{{ $meal_entry->created_at }}</td> 
    </tr> 
    @endforeach 
</tbody> 

而且記得要加

{{ $members->links() }} 

你想要分頁鏈接的地方。

相關問題