{"id":21163183,"url":"https://github.com/timgws/querybuilderparser","last_synced_at":"2025-04-12T19:48:03.262Z","repository":{"id":19224205,"uuid":"22458543","full_name":"timgws/QueryBuilderParser","owner":"timgws","description":"A simple to use query builder for the jQuery QueryBuilder plugin for use with Laravel.","archived":false,"fork":false,"pushed_at":"2024-02-26T16:28:13.000Z","size":178,"stargazers_count":161,"open_issues_count":17,"forks_count":70,"subscribers_count":11,"default_branch":"master","last_synced_at":"2025-04-12T19:47:51.905Z","etag":null,"topics":["datatables","eloquent","faceted-search","jquery-querybuilder","laravel","mongodb","query-builder"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/timgws.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2014-07-31T06:10:19.000Z","updated_at":"2025-01-20T00:31:45.000Z","dependencies_parsed_at":"2024-06-18T21:52:42.528Z","dependency_job_id":null,"html_url":"https://github.com/timgws/QueryBuilderParser","commit_stats":{"total_commits":142,"total_committers":12,"mean_commits":"11.833333333333334","dds":0.1338028169014085,"last_synced_commit":"4da94004e9f24c319ba14c15c78575f618082e80"},"previous_names":[],"tags_count":21,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/timgws%2FQueryBuilderParser","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/timgws%2FQueryBuilderParser/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/timgws%2FQueryBuilderParser/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/timgws%2FQueryBuilderParser/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/timgws","download_url":"https://codeload.github.com/timgws/QueryBuilderParser/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248625497,"owners_count":21135513,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["datatables","eloquent","faceted-search","jquery-querybuilder","laravel","mongodb","query-builder"],"created_at":"2024-11-20T13:41:18.910Z","updated_at":"2025-04-12T19:48:03.239Z","avatar_url":"https://github.com/timgws.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# QueryBuilderParser\n\nStatus Label  | Status Value\n--------------|-------------\nBuild | [![Build Status](https://travis-ci.org/timgws/QueryBuilderParser.svg?branch=master)](https://travis-ci.org/timgws/QueryBuilderParser)\nInsights | [![SensioLabsInsight](https://insight.sensiolabs.com/projects/70403e01-ad39-4117-bdef-d0c09c382555/mini.png?branch=master)](https://insight.sensiolabs.com/projects/70403e01-ad39-4117-bdef-d0c09c382555)\nCode Climate | [![Code Climate](https://codeclimate.com/github/timgws/QueryBuilderParser/badges/gpa.svg)](https://codeclimate.com/github/timgws/QueryBuilderParser)\nTest Coverage | [![Coverage Status](https://coveralls.io/repos/github/timgws/QueryBuilderParser/badge.svg?branch=master)](https://coveralls.io/github/timgws/QueryBuilderParser?branch=master)\n\n**QueryBuilderParser** is designed mainly to be used inside Laravel projects, however it can be used outside Laravel\nprojects by using Illuminate/Database.\n\nA simple to use query builder for the [jQuery QueryBuilder plugin](http://querybuilder.js.org/demo.html#plugins).\n\n# Using QueryBuilderParser\n\n## Building a new query from QueryBuilder rules.\n\n```php\n    use timgws\\QueryBuilderParser;\n\n    $table = DB::table('table_of_data_to_integrate');\n    $qbp = new QueryBuilderParser(\n        // provide here a list of allowable rows from the query builder.\n        // NOTE: if a row is listed here, you will be able to create limits on that row from QBP.\n        array( 'name', 'email' )\n    );\n\n    $query = $qbp-\u003eparse($input['querybuilder'], $table);\n\n    $rows = $query-\u003eget();\n    return Response::JSON($rows);\n```\n\n![jQuery QueryBuilder](/querybuilder.png?raw=true \"jQuery QueryBuilder\")\n\nThis query when posted will create the following SQL query:\n\n```sql\nSELECT * FROM table_of_data_to_integrate WHERE `name` LIKE '%tim%' AND `email` LIKE '%@gmail.com'\n```\n\n## Getting results from MongoDB\n```php\n    use timgws\\QueryBuilderParser;\n\n    $table = DB::collection('data');\n    $qbp = new QueryBuilderParser(\n        // provide here a list of allowable rows from the query builder.\n        // NOTE: if a row is listed here, you will be able to create limits on that row from QBP.\n        array( 'name', 'email' )\n    );\n\n    $query = $qbp-\u003eparse($input['querybuilder'], $table);\n\n    $rows = $query-\u003eget();\n    return Response::JSON($rows);\n```\n\nThis query when posted will create the following MongoDB query:\n\n```json\n    {\n      \"$and\": [\n        {\n          \"name\": {\n            \"$regex\": \"tim\"\n          }\n        },\n        {\n          \"email\": {\n            \"$regex\": \"@gmail\\\\.com$\"\n          }\n        }\n      ]\n    }\n```\n\nNote that to use this you will need to install and configure `jenssegers/mongodb`.\n\n# Integration examples\n\n## Integrating with jQuery Datatables\n\nMixed with Datatables, jQuery QueryBuilder makes for some true awesome, allowing limitless options\nfor filtering data, and seeing the results on the fly.\n\n```php\n    use timgws\\QueryBuilderParser;\n    \n    class AdminUserController {\n        function displayUserDatatable() {\n            /* builder is POST'd by the datatable */\n            $queryBuilderJSON = Input::get('rules');\n            \n            $show_columns = array('id', 'username', 'email_address');\n            \n            $query = new QueryBuilderParser($show_columns);\n            \n            /** Illuminate/Database/Query/Builder $queryBuilder **/\n            $queryBuilder = $query-\u003eparse(DB::table('users'));\n            \n            return Datatable::query($queryBuilder)\n                -\u003eshowColumns($show_columns)\n                -\u003eorderColumns($show_columns)\n                -\u003esearchColumns($show_columns)\n                -\u003emake()\n        }\n    }\n```\n\nOn the client side, a little bit of magic is required to make everything work.\n\n```js\n    // the default rules, what will be used on page loads...\n    var datatablesRequest = {};\n    var _rules = defaultRules = {\"condition\":\"AND\",\"rules\":[\n        {\"id\":\"active\",\"field\":\"active\",\"type\":\"integer\",\"input\":\"radio\",\"operator\":\"equal\",\"value\":\"1\"}\n    ]};\n\n    // a button/link that is used to update the rules.\n    function updateFilters() {\n        _rules = $('#querybuilder').queryBuilder('getRules');\n        reloadDatatables();\n    }\n\n    function filterChange() {\n        var _json = JSON.stringify( _rules );\n        datatablesRequest = { rules: _json };\n    }\n\n    filterChange();\n\n    function reloadDatatables() {\n        /* Datatables first... */\n        filterChange();\n\n        $('.dataTable').each(function() {\n            dt = $(this).dataTable();\n            dt.fnDraw();\n        })\n    }\n\n    jQuery(document).ready(function(){\n        // dynamic table\n        oTable = jQuery('.datatable').dataTable({\n            \"fnServerParams\": function(aoData) {\n                // add the extra parameters from the jQuery QueryBuilder to the Datatable endpoint...\n                $.each(datatablesRequest , function(k,v){\n                    aoData.push({\"name\": k, \"value\": v});\n                })\n            }\n        })\n    });\n```\n\n## Using JoinSupportingQueryBuilderParser\n\n`JoinSupportingQueryBuilderParser` is a version of `QueryBuilderParser` that supports building even more complex queries.\n\n```php\n    $joinFields = array(\n        'join1' =\u003e array(\n            'from_table'      =\u003e 'master',\n            'from_col'        =\u003e 'm_col',\n            'to_table'        =\u003e 'subtable',\n            'to_col'          =\u003e 's_col',\n            'to_value_column' =\u003e 's_value',\n        ),\n        'join2' =\u003e array(\n            'from_table'      =\u003e 'master2',\n            'from_col'        =\u003e 'm2_col',\n            'to_table'        =\u003e 'subtable2',\n            'to_col'          =\u003e 's2_col',\n            'to_value_column' =\u003e 's2_value',\n            'not_exists'      =\u003e true,\n        )\n    );\n\n    $table = DB::table('table_of_data_to_integrate');\n    $jsqbp = new JoinSupportingQueryBuilderParser($fields, $this-\u003egetJoinFields());\n    $test = $jsqbp-\u003eparse($json, $builder);\n```\n\nWhich will build an SQL query similar to:\n\n```sql\nselect * where exists (select 1 from `subtable` where subtable.s_col = master.m_col and `s_value` \u003c ?)\n```\n\nFor simple queries, `QueryBuilderParser` should be enough.\n\n# Exporting CSV files\n\nJust as a footnote, there are right ways to export CSV files, and there are wrong ways.\n\nFor the right way, check out the question on StackOverflow,\n[How can I output a UTF-8 CSV in PHP that Excel will read properly?](http://stackoverflow.com/a/16766198/2143004)\n\n## Reporting Issues\n\nI 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](https://github.com/timgws/QueryBuilderParser) for this project.\n\nAlternatively, fork the project and make a pull request :)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftimgws%2Fquerybuilderparser","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftimgws%2Fquerybuilderparser","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftimgws%2Fquerybuilderparser/lists"}