0

I'm trying to show a structure using as example the tables showed in this thread: Laravel Eloquent Filter By Column of Relationship

Using this I would like to show in one page (index.blade.php for example) something like this:

Category 1
    Post1.Category1
    Post2.Category1
Category 2
    Post1.Category2
    Post2.Category2
    Post3.Category2
    Post4.Category2
Category 3    
    Post1.Category3

I don't know what exactly which arrays to I need to pass from Controller.

I can get the list of Categories but how the post for each category???

Thanks

Any idea

Community
  • 1
  • 1
davisoski
  • 727
  • 2
  • 14
  • 41

1 Answers1

0

You just need to pass in the Categories with their Posts. It's pretty straightforward.

Controller

$categories = Category::with('posts')->get();
return View::make('some.view')->with('categories', $categories);

View

@foreach($categories as $category)
    {{{ $category->name }}}
    @foreach($category->posts as $post)
        {{{ $post->name }}}.{{{ $category->name }}}
    @endforeach
@endforeach
user1669496
  • 32,176
  • 9
  • 73
  • 65