2017 © Pedro Peláez
 

library query-builder-parser

Build complex Eloquent & QueryBuilder queries automatically when using jQuery-QueryBuilder

image

timgws/query-builder-parser

Build complex Eloquent & QueryBuilder queries automatically when using jQuery-QueryBuilder

  • Tuesday, April 24, 2018
  • by timgws
  • Repository
  • 7 Watchers
  • 78 Stars
  • 13,103 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 26 Forks
  • 5 Open issues
  • 11 Versions
  • 16 % Grown

The README.md

QueryBuilderParser

Status Label Status Value
Build Build Status
Insights SensioLabsInsight
Code Climate Code Climate
Test Coverage Coverage Status

QueryBuilderParser is designed mainly to be used inside Laravel projects, however it can be used outside Laravel projects by using Illuminate/Database., (*1)

A simple to use query builder for the jQuery QueryBuilder plugin., (*2)

Using QueryBuilderParser

Building a new query from QueryBuilder rules.

    use timgws\QueryBuilderParser;

    $table = DB::table('table_of_data_to_integrate');
    $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);

jQuery QueryBuilder, (*3)

This query when posted will create the following SQL query:, (*4)

SELECT * FROM table_of_data_to_integrate WHERE `name` LIKE '%tim%' AND `email` LIKE '%@gmail.com'

Getting results from MongoDB

    use timgws\QueryBuilderParser;

    $table = DB::collection('data');
    $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);

This query when posted will create the following MongoDB query:, (*5)

    {
      "$and": [
        {
          "name": {
            "$regex": "tim"
          }
        },
        {
          "email": {
            "$regex": "@gmail\\.com$"
          }
        }
      ]
    }

Note that to use this you will need to install and configure jenssegers/mongodb., (*6)

Integration examples

Integrating with jQuery Datatables

Mixed with Datatables, jQuery QueryBuilder makes for some true awesome, allowing limitless options for filtering data, and seeing the results on the fly., (*7)

    use timgws\QueryBuilderParser;

    class AdminUserController {
        function displayUserDatatable() {
            /* builder is POST'd by the datatable */
            $queryBuilderJSON = Input::get('rules');

            $show_columns = array('id', 'username', 'email_address');

            $query = new QueryBuilderParser($show_columns);

            /** Illuminate/Database/Query/Builder $queryBuilder **/
            $queryBuilder = $query->parse(DB::table('users'));

            return Datatable::query($queryBuilder)
                ->showColumns($show_columns)
                ->orderColumns($show_columns)
                ->searchColumns($show_columns)
                ->make()
        }
    }

On the client side, a little bit of magic is required to make everything work., (*8)

    // the default rules, what will be used on page loads...
    var datatablesRequest = {};
    var _rules = defaultRules = {"condition":"AND","rules":[
        {"id":"active","field":"active","type":"integer","input":"radio","operator":"equal","value":"1"}
    ]};

    // a button/link that is used to update the rules.
    function updateFilters() {
        _rules = $('#querybuilder').queryBuilder('getRules');
        reloadDatatables();
    }

    function filterChange() {
        var _json = JSON.stringify( _rules );
        datatablesRequest = { rules: _json };
    }

    filterChange();

    function reloadDatatables() {
        /* Datatables first... */
        filterChange();

        $('.dataTable').each(function() {
            dt = $(this).dataTable();
            dt.fnDraw();
        })
    }

    jQuery(document).ready(function(){
        // dynamic table
        oTable = jQuery('.datatable').dataTable({
            "fnServerParams": function(aoData) {
                // add the extra parameters from the jQuery QueryBuilder to the Datatable endpoint...
                $.each(datatablesRequest , function(k,v){
                    aoData.push({"name": k, "value": v});
                })
            }
        })
    });

Using JoinSupportingQueryBuilderParser

JoinSupportingQueryBuilderParser is a version of QueryBuilderParser that supports building even more complex queries., (*9)

    $joinFields = array(
        'join1' => array(
            'from_table'      => 'master',
            'from_col'        => 'm_col',
            'to_table'        => 'subtable',
            'to_col'          => 's_col',
            'to_value_column' => 's_value',
        ),
        'join2' => array(
            'from_table'      => 'master2',
            'from_col'        => 'm2_col',
            'to_table'        => 'subtable2',
            'to_col'          => 's2_col',
            'to_value_column' => 's2_value',
            'not_exists'      => true,
        )
    );

    $table = DB::table('table_of_data_to_integrate');
    $jsqbp = new JoinSupportingQueryBuilderParser($fields, $this->getJoinFields());
    $test = $jsqbp->parse($json, $builder);

Which will build an SQL query similar to:, (*10)

select * where exists (select 1 from `subtable` where subtable.s_col = master.m_col and `s_value` < ?)

For simple queries, QueryBuilderParser should be enough., (*11)

Exporting CSV files

Just as a footnote, there are right ways to export CSV files, and there are wrong ways., (*12)

For the right way, check out the question on StackOverflow, How can I output a UTF-8 CSV in PHP that Excel will read properly?, (*13)

Reporting Issues

I use this code in a number of my projects, so if you do find an issue, please feel free to report it with GitHub's bug tracker for this project., (*14)

Alternatively, fork the project and make a pull request :), (*15)

The Versions

24/04 2018

dev-master

9999999-dev http://github.com/timgws/QueryBuilderParser

Build complex Eloquent & QueryBuilder queries automatically when using jQuery-QueryBuilder

  Sources   Download

MIT

The Requires

 

The Development Requires

mongodb laravel sql parser jquery eloquent ajax query builder

24/04 2018

1.5

1.5.0.0 http://github.com/timgws/QueryBuilderParser

Build complex Eloquent & QueryBuilder queries automatically when using jQuery-QueryBuilder

  Sources   Download

MIT

The Requires

 

The Development Requires

mongodb laravel sql parser jquery eloquent ajax query builder

05/09 2017

1.4

1.4.0.0 http://github.com/timgws/QueryBuilderParser

Build complex Eloquent & QueryBuilder queries automatically when using jQuery-QueryBuilder

  Sources   Download

MIT

The Requires

 

The Development Requires

mongodb laravel sql parser jquery eloquent ajax query builder

31/01 2017

1.3

1.3.0.0 http://github.com/timgws/QueryBuilderParser

Build complex Eloquent & QueryBuilder queries automatically when using jQuery-QueryBuilder

  Sources   Download

MIT

The Requires

 

The Development Requires

mongodb laravel sql parser jquery eloquent ajax query builder

15/09 2016

1.2

1.2.0.0 http://github.com/timgws/QueryBuilderParser

Build complex Eloquent & QueryBuilder queries automatically when using jQuery-QueryBuilder

  Sources   Download

MIT

The Requires

 

The Development Requires

mongodb laravel sql parser jquery eloquent ajax query builder

15/05 2016

1.1.4

1.1.4.0 http://github.com/timgws/QueryBuilderParser

Build complex Eloquent & QueryBuilder queries automatically when using jQuery-QueryBuilder

  Sources   Download

MIT

The Requires

 

The Development Requires

mongodb laravel sql parser jquery eloquent ajax query builder

01/03 2016

1.1.3

1.1.3.0 http://github.com/timgws/QueryBuilderParser

Build complex Eloquent & QueryBuilder queries automatically when using jQuery-QueryBuilder

  Sources   Download

MIT

The Requires

 

The Development Requires

mongodb laravel sql parser jquery eloquent ajax query builder

23/02 2016

1.1.2

1.1.2.0 http://github.com/timgws/QueryBuilderParser

Build complex Eloquent & QueryBuilder queries automatically when using jQuery-QueryBuilder

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel sql parser jquery eloquent ajax query builder

18/01 2016

1.1.1

1.1.1.0 http://github.com/timgws/QueryBuilderParser

Build complex Eloquent & QueryBuilder queries automatically when using jQuery-QueryBuilder

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel sql parser jquery eloquent ajax query builder

12/10 2015

1.1.0

1.1.0.0 http://github.com/timgws/QueryBuilderParser

Build complex Eloquent & QueryBuilder queries automatically when using jQuery-QueryBuilder

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel sql parser jquery eloquent ajax query builder

07/09 2015

v1.0

1.0.0.0 http://github.com/timgws/QueryBuilderParser

Build complex Eloquent & QueryBuilder queries automatically when using jQuery-QueryBuilder

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel sql parser jquery eloquent ajax query builder