2016-07-28 8 views
0

私は、裁判や失敗の時間の後に私の刃のテンプレートの内側にforeachループに立ち往生して何か助けが必要です。Laravel foreach issues

マイコントローラー

public function menue() { 

    $restaurants = User::with('articles')->get();; 

    return view('pages.menues')->withRestaurants($restaurants); 
} 

私のforeach

@foreach($restaurants as $restaurant) 

     <div class="panel panel-default"> 
      <div class="panel-heading"> 
      @foreach($restaurant->articles as $article) 
       {{$article->title}} 
       <span class="float-right">{{$article->published_at}}</span> 
      @endforeach 
      </div> 

      <div class="panel-body"> 
      @foreach($restaurant->articles as $article) 
       {{$article->body}} 
      @endforeach 

      {{$restaurant->name}} 

      </div> 
     </div> 

     @endforeach 

これは私がループトラフにしようとしているものです:

{ 
     "id":1, 
     "name":"Sam", 
     "email":"[email protected]", 
     "created_at":"2016-07-26 15:03:51", 
     "updated_at":"2016-07-27 15:39:55", 
     "articles":[ 
      { 
      "id":1, 
      "user_id":1, 
      "title":"Monday Afternoon", 
      "body":"got it", 
      "created_at":"2016-07-27 15:31:05", 
      "published_at":"2016-07-27 15:30:00", 
      "excerpt":null, 
      "updated_at":"2016-07-27 15:31:05" 
      }, 
      { 
      "id":3, 
      "user_id":1, 
      "title":"Good Morning Wednesday", 
      "body":"lorem ipsum", 
      "created_at":"2016-07-27 11:38:37", 
      "published_at":"2016-07-27 11:38:00", 
      "excerpt":null, 
      "updated_at":"2016-07-27 11:38:37" 
      }, 
      { 
      "id":4, 
      "user_id":1, 
      "title":"Good Morning Thursday", 
      "body":"lorem ipsum ", 
      "created_at":"2016-07-27 11:39:14", 
      "published_at":"2016-07-28 14:38:00", 
      "excerpt":null, 
      "updated_at":"2016-07-27 11:39:14" 
      }, 
      { 
      "id":5, 
      "user_id":1, 
      "title":"Wednesday Afternoon", 
      "body":"Hallo Welt", 
      "created_at":"2016-07-27 14:55:00", 
      "published_at":"2016-07-27 14:54:00", 
      "excerpt":null, 
      "updated_at":"2016-07-27 14:55:00" 
      } 
     ] 
    } 

私のブレードTEMPLの出力ate Output of my blade

結果は4つの投稿の代わりに、私は2つの投稿しか得られません。それぞれには他の投稿が含まれています。 4つの投稿をすべて個別に表示するにはどうすればよいですか?

+0

があなたの問題に関連しないようにしよう、しかし、あなたが考慮しなければならない[イーガーローディング関係](https://laravel.com/docs/5.1/eloquent-relationships#eager-loading)の代わりに、あなたは2つの別々のforeachを持っているのですか?悪いのは、あなたは2つの別々のforeachを持っているということです。ループ) –

答えて

1

この

@foreach($restaurants as $restaurant) 
    @foreach($restaurant->articles as $article) 
    <div class="panel panel-default"> 
     <div class="panel-heading"> 
     {{$article->title}} 
     <span class="float-right">{{$article->published_at}}</span> 
     </div> 
     <div class="panel-body">{{$article->body}}</div> 
    </div> 
    @endforeach 
@endforeach 
+0

ありがとう、あなた。それはとても簡単です! –