in your blade file check condition like that
@if($category->posts_count > 0)
<a class="btn btn-success" href="{{ route('category', $category->code)}}">Open</a>
@else
<span class="btn btn-warning">No posts in this category</span>
@endif
in your controller used withCount method
$category = Category::withCount('posts')->get();
and in your category model add relationship if not added(one to many)
public function posts(){
return $this->hasMany(Post::class);
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…