Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

webfuelcode's avatar

How to get the post list via category link

Working on the category where the link will open the posts list. I am also looking to count the number of posts per category.

Controller:

public function show(Category $category)
    {
        return view('category', compact('category'));
    }

View page:

@foreach ($categories as $category)
                            <li class="list-group-item">
                                <a href="{{route('categories.show', $category->id)}}">{{$category->name}}</a>
                            </li>
                        @endforeach

The error appears 404 erroe.

0 likes
6 replies
automica's avatar

@webfuelcode if you are getting 404, it means you don't have the route defined.

Can you post the contents of routes.php?

BTW Your controller is passing a single $category through to the view but in your view you are looping through $categories. That doesn't look correct.

with regards to getting number of posts per category, you can do the following

$category->posts()->count();
webfuelcode's avatar

@automica thanks for the post count, it is fine.

And here I did not cleary about the pages. Posting again about the same...actually the function above is different and the loop coming from a different function which is index. So making things clear again here...

I am using resource in route:

Route::resource('categories', 'CategoryController', ['except' => ['create']]);

I have LinkController here:

public function index()
    {
        $links = Link::paginate(5);
        $categories = Category::all();
        return view('list', compact('links', 'categories'));
    }

And CategoryController is this:

public function show(Category $category)
    {
        //$category = $category->links;
        return view('category', compact('category'));
    }

And list.blade.php page where the foreach loop is used as in the previous post. Now I have changed it to this one:

<ul class="list-group">
                        
                        @foreach ($categories as $category)
                            
                                <a class="list-group-item d-flex justify-content-between align-item-center" href="{{route('categories.show', $category->id)}}">{{$category->name}} <span class="badge-info badge-pill">{{$category->links()->count()}}</span></a>
                            
                        @endforeach
                    </ul>

I want to set the link in this list.blade.php file which will show the post according to category. So I have created category page and it is added on CategoryCotroller. But this category page shows 404 error...whats is the mistake there...

I have not added anything on category page, just a title and trying to get the category name.

Snapey's avatar

Category Controller

public function index()
{
        $categories = Category::all();
        return view('list', compact('links', 'categories'));
}

public function show(Category $category)
{
    dd($category->toArray());
}

list.blade.php

<ul class="list-group">
                        
    @foreach ($categories as $category)
                            
        <a class="list-group-item d-flex justify-content-between align-item-center" href="{{route('categories.show', $category->id)}}">{{$category->name}} <span class="badge-info badge-pill">{{$category->links()->count()}}</span></a>
                            
    @endforeach
</ul>
  • when you go to route /categories do you see your list of categories ?

  • when you click on a category, do you see category data listed for the one you clicked on?

webfuelcode's avatar

@snapey

  • Yes, route /categories shows the list of categories.

  • This is what I am trying to do. I want to make the category clickable links to the page of related posts. But here I see 404 error.

The CategoryController index is:

public function index()
    {
        $categories = Category::all();
        $categories = Category::paginate(10);
        return view('admin.create_category', compact('categories'));
    }

And the view page I am using is:

@extends('layouts.app')

@section('content')
    <div class="container">
        @include('partials.msg')
        @include('partials.errormsg')
        <div class="row">
            <div class="col-md-9">
                <h2>Categories</h2>
                <table class="table table-hover">
                    <thead class="bg-warning">
                        <tr>
                            <th>#</th>
                            <th>Name</th>
                            <th>Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach ($categories as $category)
                        <tr>
                            <td>{{ $category->id }}</td>
                            <td><a href="{{ route('categories.show', $category->id) }}">{{ $category->name }}</a></td>
                            <td>
                                <!-- Edit button -->
                                <div class="float-left">
                                    <button type="button" class="btn btn-success" data-toggle="modal" data-target="#modal{{$category->id}}">
                                        Edit
                                    </button>
                                </div>
                                @include('admin.partials.edit_category')
                            </td>
                        </tr>
                        @endforeach
                    </tbody>
                </table>

            </div>
            
            <div class="col-md-3">
                <form action="{{route('categories.store')}}" method="POST" class="card bg-danger text-white p-2">
                    @csrf
                    <div class="form-group">
                      <label for="name">Category name</label>
                      <input type="text" name="name" class="form-control" placeholder="Enter new category">
                    </div>
                    <button type="submit" class="btn btn-outline-light btn-lg btn-block">Submit</button>
                  </form>

                  <div class="card border-info my-2">
                    <div class="card-header">
                        Site operation
                    </div>
                    <div class="card-body">
                        
                        <div class="card-link">
                          <a class="btn btn-outline-warning btn-lg btn-block" href="{{ route('link.index') }}">See the list</a>
                          <a class="btn btn-outline-info btn-lg btn-block" href="{{ route('link.create') }}">Add new post</a>
                        </div>
                    </div>
                  </div>
    
            </div>
        </div>
    </div>
@endsection
Snapey's avatar

OK, you don't have to follow my suggestions.

By the way, this

    $categories = Category::all();
    $categories = Category::paginate(10);

is just silly. Do you want a list of all categories or do you want to paginate them? Use one or the other.

Please or to participate in this conversation.