Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

balint's avatar

QueryBuilder howto

hi all! can someone give me an example or a step-by-step guide, how can I integrate this package into a laravel app? https://packagist.org/packages/timgws/query-builder-parser#dev-master

0 likes
12 replies
Vilfago's avatar

It's hard to do better than the link you gave.

Open terminal, write composer require timgws/query-builder-parser, then press enter, and enjoy

balint's avatar

@VILFAGO - awesome! Can someone help me with an example of a controller and view config?

Vilfago's avatar

There is everything on packagist... just scroll with mouse wheel

balint's avatar

@VILFAGO - it is not working for me. that's why I asked for examples....

Vilfago's avatar

So, what did you try, and what is not working ? Could you share some code and errors ?

Try in terminal composer dump-autoload and php artisan cache:clear

balint's avatar

how should I define input variable? I got ""Undefined variable: input"" error

controller:

<?php

namespace App\Http\Controllers\Query;

use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Response;
use timgws\QueryBuilderParser;
use Illuminate\Support\Facades\Input;


class BuilderController extends Controller
{
    public function querybuilder ()
    {
        $queryBuilderJSON = Input::get('rules');
        $table = DB::table('users');
        $qbp = new QueryBuilderParser(
        // provide here a list of allowable rows from the query builder.
        // NOTE: if a row is listed here, you will be able to create limits on that row from QBP.
            array('name', 'email')
        );

        $query = $qbp->parse($input['querybuilder'], $table);

        $rows = $query->get();
        return Response::JSON($rows);
    }
    
}

view

@extends('app')
@section('title', 'Query Builder')


    <link rel="stylesheet" href="/node_modules/bootstrap/dist/css/bootstrap.css"/>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/jQuery-QueryBuilder/dist/css/query-builder.default.min.css"/>

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

<script>
    var rules_basic = {
        condition: 'AND',
        rules: [{
            id: 'price',
            operator: 'less',
            value: 10.25
        }, {
            condition: 'OR',
            rules: [{
                id: 'category',
                operator: 'equal',
                value: 2
            }, {
                id: 'category',
                operator: 'equal',
                value: 1
            }]
        }]
    };

    $('#builder-basic').queryBuilder({
        plugins: ['bt-tooltip-errors'],

        filters: [{
            id: 'name',
            label: 'Name',
            type: 'string'
        }, {
            id: 'category',
            label: 'Category',
            type: 'integer',
            input: 'select',
            values: {
                1: 'Books',
                2: 'Movies',
                3: 'Music',
                4: 'Tools',
                5: 'Goodies',
                6: 'Clothes'
            },
            operators: ['equal', 'not_equal', 'in', 'not_in', 'is_null', 'is_not_null']
        }, {
            id: 'in_stock',
            label: 'In stock',
            type: 'integer',
            input: 'radio',
            values: {
                1: 'Yes',
                0: 'No'
            },
            operators: ['equal']
        }, {
            id: 'price',
            label: 'Price',
            type: 'double',
            validation: {
                min: 0,
                step: 0.01
            }
        }, {
            id: 'id',
            label: 'Identifier',
            type: 'string',
            placeholder: '____-____-____',
            operators: ['equal', 'not_equal'],
            validation: {
                format: /^.{4}-.{4}-.{4}$/
            }
        }],

        rules: rules_basic
    });

    $('#btn-reset').on('click', function() {
        $('#builder-basic').queryBuilder('reset');
    });

    $('#btn-set').on('click', function() {
        $('#builder-basic').queryBuilder('setRules', rules_basic);
    });

    $('#btn-get').on('click', function() {
        var result = $('#builder-basic').queryBuilder('getRules');

        if (!$.isEmptyObject(result)) {
            alert(JSON.stringify(result, null, 2));
        }
    });
</script>
<script src="/public/js/jquery-2.1.3.min.js"></script>
<script src="/node_modules/bootstrap/dist/js/bootstrap.js"></script>
<script src="https://cdn.jsdelivr.net/npm/jQuery-QueryBuilder/dist/js/query-builder.min.js"></script>
<script src="/public/js/app.js"></script>
Vilfago's avatar

Try :

public function querybuilder ()
    {
        $queryBuilderJSON = Input::get('rules');
        $table = DB::table('users');
        $qbp = new QueryBuilderParser(
        // provide here a list of allowable rows from the query builder.
        // NOTE: if a row is listed here, you will be able to create limits on that row from QBP.
            array('name', 'email')
        );

        $query = $qbp->parse($queryBuilderJSON, $table);

        $rows = $query->get();
        return Response::JSON($rows);
    }
balint's avatar

@VILFAGO - Hi! Now I got "JSON parsing threw an error: Syntax error" error. The query is not valid JSON

balint's avatar

@VILFAGO - file: /Users/****/Documents/GitHub/app/vendor/timgws/query-builder-parser/src/QueryBuilderParser/QBPFunctions.php

log: [2019-01-30 08:52:28] testing.ERROR: JSON parsing threw an error: Syntax error {"exception":"[object] (timgws\QBParseException(code: 0): JSON parsing threw an error: Syntax error at /Users/****/Documents/GitHub/app/vendor/timgws/query-builder-parser/src/QueryBuilderParser/QBPFunctions.php:216)

Vilfago's avatar

Ok, so I guess what line 216 is.

public function querybuilder ()
    {
        $queryBuilderJSON = Input::get('rules');
        $table = DB::table('users');
        $qbp = new QueryBuilderParser(
        // provide here a list of allowable rows from the query builder.
        // NOTE: if a row is listed here, you will be able to create limits on that row from QBP.
            array('name', 'email')
        );

        $query = $qbp->parse(json_encode($queryBuilderJSON), $table);

        $rows = $query->get();
        return Response::JSON($rows);
    }
balint's avatar

@VILFAGO - line 216 is: "throw new QBParseException('JSON parsing threw an error: '.json_last_error_msg());" see below


   private function decodeJSON($json)
    {
        $query = json_decode($json);

        if (json_last_error()) {
            throw new QBParseException('JSON parsing threw an error: '.json_last_error_msg());
        }

        if (!is_object($query)) {
            throw new QBParseException('The query is not valid JSON');
        }

        return $query;
    }

I've changed the controller, but it is still the same. "The query is not valid JSON"

Please or to participate in this conversation.