Danny971's avatar

Can't get jQuery QueryBuilder to load in my Laravel + Livewire project

Hi everyone,

I'm trying to use jQuery QueryBuilder in my Laravel 10 app (with Livewire), but I can't get it to load — I keep getting undefined when I check $.fn.queryBuilder in the browser console.

Here’s what I have so far:

Blade Layout Head:

<!-- jQuery -->
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>

<!-- QueryBuilder CSS -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/query-builder.default.min.css">

<!-- QueryBuilder JS -->
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/query-builder.standalone.min.js"></script>


Livewire Component View 

<div id="builder"></div>

@push('scripts')
<script>
    $(document).ready(function () {
        console.log(typeof $.fn.queryBuilder); // Always returns undefined

        $('#builder').queryBuilder({
            filters: [
                { id: 'gender', label: 'Gender', type: 'string' }
            ]
        });
    });
</script>
@endpush

I tried both CDN and downloading the files manually to public/js and public/css, but I still get undefined. I’m not sure if it’s:

A loading order issue (maybe jQuery loads too late?)

A Livewire issue (because this is inside a Livewire component view)

A Laravel asset issue (maybe I need to use @vite instead of asset()?)

No console errors other than queryBuilder being undefined.

Has anyone successfully used jQuery QueryBuilder in a Laravel + Livewire setup? Do I need to initialize it differently (e.g. in window.onload or Livewire.hook())?

Any advice is appreciated!

1 like
6 replies
vincent15000's avatar

Hmmm ... I don't recommend to use jQuery if you are using Livewire (which also comes with AlpineJS) because of eventual conflicts between the libraries.

Why not just use AlpineJS instead of jQuery ?

Danny971's avatar

I’m considering using QueryBuilder.js because I want to allow users (or myself) to query data in a visual and user-friendly way. The goal is to make building queries easier and then use those queries to generate reports

https://querybuilder.js.org/

1 like
Tray2's avatar

Like @vincent15000 says, don't use jQuery together with Livewire.

However the probable reason it doesn't work is that you are using Vite, and to be able to use functions in JavaScript they need to be exported in the source file, and then imported in the file that uses it.

Building something like what you need is very easy in Livewire.

A quick search on Google gave me this.

https://medium.com/@Orpheric_Codeur/how-to-create-a-real-time-search-filter-with-laravel-livewire-f566d36fabad

1 like
Danny971's avatar

can I use react query builder as an alternative ?

1 like
Tray2's avatar

If you are using react yes, but mixing react with Livewire probably isn't a good idea.

I would suggest sticking to Livewire if you are already using it.

1 like

Please or to participate in this conversation.