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
]);
}
}