Skip to main content

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

Read more here

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? (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

Y
Yuri ✓ Link copied!

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?

N
Nerijus ✓ Link copied!

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" />
Y
Yuri ✓ Link copied!

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

J
jwinder ✓ Link copied!

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

KA
KHALID ABDULMAJEED ✓ Link copied!

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

KA
KHALID ABDULMAJEED ✓ Link copied!

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

N
Nerijus ✓ Link copied!

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

KA
KHALID ABDULMAJEED ✓ Link copied!

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

KA
KHALID ABDULMAJEED ✓ Link copied!

what should I do? I am new in this website

KA
KHALID ABDULMAJEED ✓ Link copied!

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

N
Nerijus ✓ Link copied!

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

KA
KHALID ABDULMAJEED ✓ Link copied!

Thanks :)