Steve Brown
Steve Brown

Reputation: 447

Laravel foreach issues

I get stuck on a foreach loop inside my blade template after hours of trial and failure I need some help.

My Controller

public function menue() {

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

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

My 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

This is what I'm trying to loop trough:

    {  
       "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"
          }
       ]
    }

Output of my blade template Output of my blade

The result is instead of four posts I get only 2 posts. Each of them contains to other posts. How can I display all 4 posts individually in my view?

Upvotes: 0

Views: 159

Answers (1)

DsRaj
DsRaj

Reputation: 2328

Try This

@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

Upvotes: 1

Related Questions