Summer Sale! All accounts are 50% off this week.

Marcolino922's avatar

Show category list and related posts

Hi, I would like to get a list of categories where the related posts are shown by clicking.

Right now my code is this:

Categories Model

    public function items(){
        return $this->belongsToMany(Items::class);
    }

@foreach($categories as $cat)
       @foreach($cat->items as $item)
    
            {{ $item->title }}

      @endforeach
@endforeach
0 likes
3 replies
esorone's avatar

Hello,

Any errors and are you able to show the controller as well?

Btw: Next to this there are some creat series on this topic.. With these examples, you are able to debug your app quite easily. Just as a tip! I know looking to the series cost time, but you will earn it back for sure.

Marcolino922's avatar

I think I have solved this

    public function items(){
        return $this->hasMany(Items::class, 'category_id', 'id');
    }
Marcolino922's avatar

I was wrong, with the above post report only one post belonging to that category is taken.

Who can help me? Thank you

Please or to participate in this conversation.