Skip to main content

Black Friday 2025! Only until December 1st: coupon FRIDAY25 for 40% off Yearly/Lifetime membership!

Read more here

Category Dropdown: Filtering Data

Premium
3 min read

Now that we have a select input for the category, let's make it work as a filter. After selecting a category, the list should show only the posts that only belong to that category.

selected category filter


Vue: Watch the Selected Category

To get all the posts that belong to a category, we need to watch the selectedCategory variable. First, we need to import watch from the Vue.

resources/js/components/Posts/Index.vue:

<template>
// ...
</template>
 
<script setup>
import { onMounted, ref } from "vue";
import { onMounted, ref, watch } from "vue";
import { TailwindPagination } from 'laravel-vue-pagination';
import usePosts from "@/composables/posts";
import useCategories from "@/composables/categories";
// ...
</script>

Now using this watch() function, we can watch the selectedCategory variable for the changes with two values: current and previous.

Inside watch we can call the...

The Full Lesson is Only for Premium Members

Want to access all of our courses? (29 h 14 min)

You also get:

54 courses
Premium tutorials
Access to repositories
Private Discord
Get Premium for $129/year or $29/month

Already a member? Login here

Comments & Discussion

R
Rangoh ✓ Link copied!

Really straightforward example of using Vue watchers.

KA
KHALID ABDULMAJEED ✓ Link copied!

Hello, I would like to make filter data without any relationship between Category and Post so I have PostController and I want to pass the query request into that

$posts = Post::with('category') ->when(request('category'), function (Builder $query) { $query->where('category_id', request('category')); })->paginate(10);

					can you help me please?
KA
KHALID ABDULMAJEED ✓ Link copied!

I want to make multi filter data what should I do :)?

BO
Bokan Omar ✓ Link copied!

Hi, sir , when I use where with the function Builder it highlight the where clause with redline but still working what is the problem of red underline . $posts = Post::with('category')->when(request('category'), function (Builder $builder){ $builder->where('category_id', request('category')); })->paginate(3);

N
Nerijus ✓ Link copied!

Hard to tell why it underlines, might be your ide