Skip to main content

Category ToggleGroup and Tasks Filter

Premium
10 min read

This lesson will tackle two things: choosing categories for the tasks in the form/table and filtering the tasks by category.


Create Task Form: Choose Category

First, we need to modify the Controller to use Categories. I will add the code for both Create and Edit forms right away.

app/Http/Controllers/TaskController.php

use App\Models\TaskCategory;
 
// ...
 
public function create()
{
return Inertia::render('Tasks/Create');
return Inertia::render('Tasks/Create', [
'categories' => TaskCategory::all(),
]);
}
 
// ...
 
public function edit(Task $task)
{
$task->load(['media']);
$task->load(['media', 'taskCategories']);
$task->append('mediaFile');
 
return Inertia::render('Tasks/Edit', [
'task' => $task,
'categories' => TaskCategory::all(),
]);
}
// ...

Next, we need to add task_categories to the...

The Full Lesson is Only for Premium Members

Want to access all of our courses? (30 h 09 min)

You also get:

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

Already a member? Login here

Comments & Discussion

P
pero ✓ Link copied!

Hello, you saved lots of working hours with the paginatin component :) Thx!

But it is not working with the category filter. To make it work, I did the following:

In the index method of TaskController I added "->withQueryString()" after "->paginate(10)"

In Pagination.vue I changed v-on:click="() => router.visit(props.resource.path + '?page=' + item.value)" to: v-on:click="() => router.visit(props.resource.links[index+1].url)

M
Modestas ✓ Link copied!

Interesting, I might have missed this case when doing the testing!

But at least the solution isn't too complicated, so I'll update the article if it's needed.

Thank you for letting us know!

P
pero ✓ Link copied!

The problem was, that the pagination pages' links missing the categorieId values from the query string. So if you filter for a category and jump to page 2, the filter disappears

M
Modestas ✓ Link copied!

You are right - this was indeed the bug!

I am updating the article and checking the react version for the same problem.

Thank you very much!

P
pero ✓ Link copied!

Hi, there is still a bug here :)

v-on:click="() => router.visit(props.resource.links[index+1].url

The above code is not working if there are lots of pages, because the "..." placeholders in the links array:

{ "url":null, "label":"...", "active":false },

Working code is:

v-on:click="() => router.visit(props.resource.links.find((link) => link.label == item.value).url)"

M
Modestas ✓ Link copied!

Thanks for letting me know, I'll update the example and repo with the new Tailwind 4 release :)

RS
Riley Shannon ✓ Link copied!

Is there any potential to adding a section on ways to add a live search to the CRUD table? Or have any resources that can start me in the right direction?

M
Modestas ✓ Link copied!

Could you explain what do you mean by live search?

RS
Riley Shannon ✓ Link copied!

Sorry!

I mean the ability to add a search box to filter results on the table. Like searching for a name or email or similar.

M
Modestas ✓ Link copied!

We have a lesson on how to do this, but without inertia (raw Vue). It would be similar in this case, just by auto-submitting a search form with GET method:

https://laraveldaily.com/lesson/vue-laravel-vite-spa-crud/table-filters-column-global

RS
Riley Shannon ✓ Link copied!

Perfect. Thank you!

A
Alexandre ✓ Link copied!

Did you notice that this image has an underscore under the "Tasks" menu item; while this other image and this another image have not?

All three images refer to the route /tasks; however only the first one is 'unfiltered' and considered to be the active route.

If you think this behavior is perhaps not the best, you may just change one line in this file:

js/resources/components/AppHeader.vue

const isCurrentRoute = computed(() => (url: string) => page.url.split('?')[0] === url);

This way, /tasks will have its menu item underlined whether there are filter(s) applied or not; and whether there is a specified page for pagination, or not.

M
Modestas ✓ Link copied!

That's a good catch!

K
Kaspars ✓ Link copied!

Hello, What can be the reason of issue, if I can not get categories in the Tasks/Edit.Vue in const form = useForm? So here categories are missing in the Object: categories: task.task_categories.map((category) => category.id), Thanks in advance

M
Modestas ✓ Link copied!

You might have forgotten to add $task->load('task_categories') inside of your controller

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.