2016-07-27 15 views
0

私の見解ではいくつかのデータを表示しようとしていますが、多次元配列をループすることは考えていません。関係テーブルからの雄弁なデータ

私のコントローラは次のとおりです。

public function menue() { 
    $articles = Article::nowpublished()->get(); 
    //$restaurants = User::all(); 
    $restaurants = User::with('articles')->get();; 


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

これは私が私の見解で何をすべきかです:

@foreach($restaurants as $restaurant) 

      <div class="panel panel-default"> 
       <div class="panel-heading"> 
       <span>{{$restaurant}}</span> 
       <span class="float-right">{{$restaurant->published_at}}</span> 
       </div> 

       <div class="panel-body"> 
        {{$restaurant->body}} 
       </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" 
     } 
    ] 
} 

私の質問はどのようにすることができますです私の$レストラン変数内の "articles"配列にアクセス/表示しますか?

答えて

0

あなたは作成することができます。

@foreach($restaurant->articles as $article) 
    {{$article->id}} 
@endforeach 
+0

を私が$いるレストラン>名前とまたあなたのforeachのをよく見ると、あなたが{{$いるレストラン>アクセスしている記事・アレイ –

+0

の内部プロパティにアクセスする必要がありますbody}}は子配列であり、foreachループは1回だけループします。体はあなたの出力に見られるように3倍の鍵です。 foreachが1回だけループされたとき、{{$ restaurant-> body}}は利用可能な3から取得するボディを知りません。なぜ私は上記のように別のforeachループが必要です。 –