Skip to main content

Extra Table Filters: Column and Global

Premium
11 min read

In this last lesson of the Full CRUD of Posts section, let's add more filters to the posts table. We will add a filter for each column and another search input to search in all columns.

finished table with extra filters


Search in Every Column

Let's start this lesson from the back-end. We need to add more condinional clauses for each column. Also, we will add a prefix search_ to each search variable.

app/Http/Controllers/Api/PostController.php:

use Illuminate\Database\Eloquent\Builder;
 
class PostController extends Controller
{
public function index()
{
$orderColumn = request('order_column', 'created_at');
if (! in_array($orderColumn, ['id', 'title', 'created_at'])) {
$orderColumn = 'created_at';
}
$orderDirection = request('order_direction', 'desc');
if (! in_array($orderDirection, ['asc', 'desc'])) {
$orderDirection = 'desc';
}
 
$posts = Post::with('category')
->when(request('category'), function (Builder $query) {
$query->where('category_id', request('category'));
->when(request('search_category'), function (Builder $query) {
$query->where('category_id', request('search_category'));
})
->when(request('search_id'), function (Builder $query) {
$query->where('id', request('search_id'));
})
->when(request('search_title'), function (Builder $query) {
$query->where('title', 'like', '%' . request('search_title') . '%');
})
->when(request('search_content'), function (Builder $query) {
$query->where('content', 'like', '%' . request('search_content') . '%');
})
->orderBy($orderColumn, $orderDirection)
->paginate(10);
 
return PostResource::collection($posts);
}
// ...
}

Now in the posts Composable, we need to pass those parameters.

resources/js/composables/posts.js:

import { ref, inject } from 'vue'
import { useRouter } from 'vue-router'
 
export default function usePosts() {
const posts = ref({})
const post = ref({})
const router = useRouter()
const validationErrors = ref({})
const isLoading = ref(false)
const swal = inject('$swal')
 
const getPosts = async (
page = 1,
category = '',
search_category = '',
search_id = '',
search_title = '',
search_content = '',
order_column = 'created_at',
order_direction = 'desc'
) => {
axios.get('/api/posts?page=' + page +
'&category=' + category +
'&search_category=' + search_category +
'&search_id=' + search_id +
'&search_title=' + search_title +
'&search_content=' + search_content +
'&order_column=' + order_column +
'&order_direction=' + order_direction)
.then(response => {
posts.value = response.data;
})
}
// ...
}

Next, we need to add the same variables in...

The Full Lesson is Only for Premium Members

Want to access all of our courses? (36 h 00 min)

You also get:

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

Already a member? Login here

Yuri avatar

Hi. I found a pagination bug. We added search_category to getPosts: <TailwindPagination :data="posts" @pagination-change-page="page => getPosts(page, search_category)" class="mt-4"/> and it works when we filtering by some category with many posts.

But it doesnt work when we filtering by title or content or global. I posted many posts with words "test" and a pagination shows me incorect numbers of pages when I filtering by a "test" word. How to fix that?

Nerijus avatar

Add every value to the getPosts in the pagination and it should work.

<TailwindPagination :data="posts" @pagination-change-page="page => getPosts(page, search_category, search_id, search_title, search_content, search_global, orderColumn, orderDirection)" class="mt-4" />
Yuri avatar

Thanks. It works now. I don't understand how, but I tried this way before asked the question and it didn't work... Magic =)

jwinder avatar

@Yuri thanks for that man, I was going crazy thinking i was the only one who noticed this!

KHALID ABDULMAJEED avatar

I followed every step in this course but when I use search I got wrong result need help plz

KHALID ABDULMAJEED avatar

I try with this query "$posts = Post::with('project')->where('category_id', '=' , request('search_category')) " and it works but I need multi filters

Nerijus avatar

Sorry, but in the comments it's impossible to help you.

KHALID ABDULMAJEED avatar

$posts = Post::with('project')->where('category_id', '=' , request('search_category'))

KHALID ABDULMAJEED avatar

what should I do? I am new in this website

KHALID ABDULMAJEED avatar

it works when re-arrange the inside getPosts(...) can you explain me watch(search_id, (current, previous) => { getPosts( 1, search_category.value, current, search_title.value, search_content.value ) }) watch(search_title, (current, previous) => { getPosts( 1, search_category.value, search_id.value, current, search_content.value ) }) watch(search_content, (current, previous) => { getPosts( 1, search_category.value, search_id.value, search_title.value, current ) })

Nerijus avatar

You have two options:

  1. Go to laravel daily discord and ask there.
  2. Go to forums like laracasts

In both options when showing code format it so it would be readable using markdown. Use three backtick and language name like ``` php

KHALID ABDULMAJEED avatar

Thanks :)

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.