Wednesday, March 8, 2017

Laravel Pass Comments with Posts

I have a One to Many relationship set up for my "announcements" having many "comments".

Currently, when my user loads up the app page, I have it send the 30 most recent announcements like such:

Route::get('/app', function () {

  $posts =      Announcement::take(30)->orderBy('id', 'desc')->get();

  return View::make('app')->with([
    //posts
    'posts'       => $posts,
    //orders
    'orders'      => $orders
  ]);

When I echo out the announcements in blade using a foreach loop through the $posts object, I want to also echo out the comments for each post under the respective post.

Is it possible to pass the comments for a post as part of the actual post object? For example, it would be nice if I could do this:

    @foreach ($posts as $post)
      //echo out the post
        
      //echo out the comments relating to this post
        
    @endforeach



via Vranvs

Advertisement