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

ThePoet444's avatar

Frontend advice for post by category sorting

Greeting Artisans!

I have a 1-n with posts and categories. Each post belongs to one category. In my view I am listing each category, then making a route to a filter to retrieve all the posts for that category. It works fine. Does what I need it to do. I however, does a full page reload every time. That is not good. (it's not a huge deal mind you, but it's not good)

public function filter($id)
    {
        $posts = Post::where('category_id', $id)->where('published_at', '<=', \Carbon\Carbon::now())->orderby('id', 'desc')->with('user')->paginate(5);
        $categories = Category::all();

        return view('post.index', compact('posts','categories'));
    }

Is this the best way? Probably not. That being said, I don't know the front end as well as I should and would like to learn. There are many different options out there, but currently I'm working with the TALL stack, so I'm hoping someone has an idea of where to begin. My google-fu is not up to the task for searching the proper terms and keywords to get me started. I would love any advice on how someone else would accomplish this task. I'm not looking for code, I'm looking for "search for x,y, and z terms in livewire/vue/whatever" type of thing. Someplace to get my foot in the door to start learning.

I actually have an issue starting with basics and moving up. It never seems to click with me. I find I learn better at starting at the advanced level and working backwards. My brain is wired funny.

Anyway, thank you for your time and any tips you can give!

0 likes
1 reply
ThePoet444's avatar

Well, after a crash course in livewire I think I have accomplished what I wanted to do. Though it was quickly put together and functions how I want, I'm not sure it's done with best practices in mind. I'd love a point and laugh and then some advice on making this better.

Thank you for taking the time to read this.

<?php

namespace App\Http\Livewire;

use App\Models\Category;
use App\Models\Post;
use Carbon\Carbon;
use Livewire\Component;
use Livewire\WithPagination;

class NewsPosts extends Component
{
    use WithPagination;

    public $filterId = '0'; //category ID default all
    private $posts;

    public function filterBy($id)
    {
        $this->filterId = $id;
    }

    public function hydrateFilterId()
    {
        $this->resetPage();
    }

    public function render()
    {
        if($this->filterId == 0 ) {
            $this->posts = Post::where('published_at', '<=', Carbon::now())
                ->orderby('id', 'desc')
                ->with('user')
                ->paginate(5);
        }else{
            $this->posts = Post::where('category_id', $this->filterId)
                ->where('published_at', '<=', Carbon::now())
                ->orderby('id', 'desc')
                ->with('user')
                ->paginate(5);
        }

        $categories = Category::all();
        return view('livewire.news-posts',[
            'categories' => $categories,
            'posts' => $this->posts
        ]);
    }
}

Please or to participate in this conversation.