Download the PHP package asseco-voice/laravel-json-query-builder without Composer

On this page you can find all versions of the php package asseco-voice/laravel-json-query-builder. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package laravel-json-query-builder

Laravel JSON query builder

This package enables building queries from JSON objects following the special logic explained below.

Installation

Install the package through composer. It is automatically registered as a Laravel service provider.

composer require asseco-voice/laravel-json-query-builder

Usage

In order to use the package, you need to instantiate JsonQuery() providing two dependencies to it. One is Illuminate\Database\Eloquent\Builder instance, and the other is a JSON/array input.

Once instantiated, you need to run the search() method, and query will be constructed on the provided builder object.

Dev naming conventions for this package

Parameter breakdown

Parameters follow a special logic to query the DB. It is possible to use the following JSON parameters (keys):

Search

The logic is done in a "column": "operator values" fashion in which we assume the following:

Main operators

Example:

Will perform a SELECT * FROM some_table WHERE first_name IN ('foo1', 'foo2') AND last_name NOT IN ('bar1', 'bar2').

In case you pass a single value for a column on string type, LIKE operator will be used instead of IN. For Postgres databases, ILIKE is used instead of LIKE to support case-insensitive search.

Will perform a SELECT * FROM some_table WHERE first_name LIKE 'foo' AND last_name NOT LIKE 'bar'.

Micro operators

Will perform a SELECT * FROM some_table WHERE first_name NOT LIKE 'foo' AND last_name LIKE 'bar%'.

Notice that here !value behaved the same as != main operator. The difference is that != main operator negates the complete list of values, whereas the !value only negates that specific value. I.e. !=value1;value2 is semantically the same as =!value1;!value2.

Logical operator example:

Will perform SELECT * FROM some_table WHERE first_name IN ('foo') OR first_name IN ('bar').

Note that logical operators are using standard bool logic precedence, therefore x AND y OR z AND q is the same as (x AND y) OR (z AND q).

Nested relation searches

You can nest another search object if the key used is a relation name which will execute a whereHas() query builder method.

I.e.

Returns

Using a returns key will effectively only return the fields given within it. This operator accepts an array of values or a single value.

Example:

Return single value:

Will perform a SELECT first_name FROM ...

Return multiple values:

Will perform a SELECT first_name, last_name FROM ...

Order by

Using order_by key does an 'order by' based on the given key(s). Order of the keys matters!

Arguments are presumed to be in a "column": "direction" fashion, where direction MUST be asc (ascending) or desc (descending). In case that only column is provided, direction will be assumed to be an ascending order.

Example:

Will perform a SELECT ... ORDER BY first_name asc, last_name desc

Group by

Using group_by key does an 'group by' based on the given key(s). Order of the keys matters!

Arguments are presumed to be a single attribute or array of attributes.

Since group by behaves like it would in a plain SQL query, be sure to select the right fields and aggregate functions.

Example:

Will perform a SELECT ... GROUP BY last_name, first_name

Relations

It is possible to load object relations as well by using relations parameter. This operator accepts an array of values or a single value.

Simple

Example:

Resolve single relation:

Resolve multiple relations:

Relations, if defined properly and following Laravel convention, should be predictable to assume:

It is possible to recursively load relations using dot notation:

This will load media relations as well as resolve media types right away. If you have the need to resolve multiple second level relations you can provide an array of those:

This will load media relations together with resolved type and category for each media object.

It is also possible to stack relations using dot notation without a limit. It must be taken into account though that this can seriously hurt performance!

Complex

Relation can also be an object with nested search attributes to additionally filter out the given result set.

Example:

will load a media relationship on the object, but only returning objects whose media_type_id equals to 1.

Limit

You can limit the number of results fetched by doing:

This will do a SELECT * FROM table LIMIT 10.

Offset

You can use offset to further limit the returned results, however it requires using limit alongside it.

This will do a SELECT * FROM table LIMIT 10 OFFSET 5.

Count

You can fetch count of records instead of concrete records by adding the count key:

This will do a SELECT count(*) FROM table.

Soft deleted

By default, soft deleted records are excluded from the search. This can be overridden with soft_deleted:

Doesn't have relations

In case you want to only find entries without a specified relation, you can do so with doesnt_have_relations key:

If you want to specify multiple relations, you can do so in the following way:

Top level logical operators

Additionally, it is possible to group search clauses by top-level logical operator.

Available operators:

Using no top-level operator will assume AND operator.

Examples

These operators take in a single object, or an array of objects, with few differences worth mentioning. Single object will apply the operator on given attributes:

Resulting in . Whereas an array of objects will apply the operator between array objects, not within the objects themselves:

Resulting in (id=1 AND name=foo) OR (id=2 AND name=bar). This is done intentionally, default operator is AND, thus it will be applied within objects.

If you'd like inner attributes changed to OR instead, you can go recursive:

Resulting in (id=1 OR name=foo) OR (id=2 AND name=bar).

Absurd examples

Since logic is made recursive, you can go as absurd and deep as you'd like, but at this point it may be smarter to revise what do you actually want from your life and universe:

Breakdown:

Result: (id=2 OR id=3) AND name=foo

Result: id=1 AND (name LIKE foo% AND name LIKE %bar)

Result: (step1) OR (step2)

Result: (step3) AND (step4)

Result: (step5) OR love<3 OR recursion=rrr

The final query (kill it with fire):

((((id=2 OR id=3) AND name=foo) OR (id=1 AND (name LIKE foo% AND name LIKE %bar))) AND we=cool) OR love<3 OR recursion=rrr

Config

Aside from standard query string search, it is possible to provide additional package configuration.

Publish the configuration by running php artisan vendor:publish --provider="Asseco\JsonQueryBuilder\JsonQueryServiceProvider".

All the keys within the configuration file have a detailed explanation above each key.

Package extensions

Once configuration is published you will see several keys which you can extend with your custom code.


All versions of laravel-json-query-builder with dependencies

PHP Build Version
Package Version
Requires php Version ^8.1
laravel/framework Version ^10.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package asseco-voice/laravel-json-query-builder contains the following files

Loading the files please wait ....