Download the PHP package bjerke/api-query-builder without Composer
On this page you can find all versions of the php package bjerke/api-query-builder. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download bjerke/api-query-builder
More information about bjerke/api-query-builder
Files in bjerke/api-query-builder
Package api-query-builder
Short Description A query builder for Laravel that parses the request and uses Eloquent ORM to query database
License MIT
Informations about the package api-query-builder
Laravel ApiQueryBuilder
A library to enable clients to query models in a very dynamic way, mimicking the Eloquent ORM.
Installation
Configuration
There is not much to configure, but one thing that can be configured is what collation to use when querying with localized order clauses.
It is preconfigured to use utf8mb4_swedish_ci
for the sv
and sv-SE
locales. If you don't need any special collations for your other locales, there's no need to publish this configuration.
If you do want to add other collations for specific locales however, you need to publish the configuration file from this library so you can change it in your own application.
To do this run the following artisan command:
You will now have a querybuilder.php
config file in /config
where you can add additional locales => collation combinations
Usage
To use the query builder, you will use the 2 main components of this library. The trait QueryBuilderModelTrait
and the builder itself QueryBuilder
.
The trait is there to help the builder validate requested fields, relations, appendable attributes and counts. As well as some helper methods. Read more on how to use counts in their own descriptions below. This trait needs to be included in the models you want to use the query builder on.
In your controller method, you can then use the query builder to compile an Eloquent builder class based on the request like:
Available query methods
Most methods include an or
counterpart, that will allow you to create OR statements in your queries. Just like Eloquent.
For example where
and orWhere
.
- where
- whereIn
- whereNotIn
- whereBetween
- whereNotBetween
- whereNull
- whereNotNull
- whereHas
- whereDoesntHave
- whereDate (whereDate / whereMonth / whereDay / whereYear / whereTime),
- search
- select
- orderBy
- groupBy
- limit
- with
- appends
- counts
- pagination
where / orWhere
Executes a where statement. It can be defined in a couple of ways.
The following will do an exact match where first_name
equals test
You can also do more advanced matching by defining an operator (=
, !=
, like
, >
, <
). When defining an operator you also need to define a value
parameter.
The following will perform a like
query matching on %test%
These methods are recursive. Meaning you can wrap multiple statements in a parent "where" to match all statements in it.
whereIn / orWhereIn
Similar to where / orWhere, but matches a list of values. Values can be defined as a comma-separated string or as an actual array.
or
whereNotIn / orWhereNotIn
Same as whereNotIn / orWhereNotIn, but matches the absence of provided values.
whereBetween / orWhereBetween
Matches column value is between the 2 provided values. Values can be defined as a comma-separated string or as an actual array.
or
whereNotBetween / orWhereNotBetween
Same as whereBetween / orWhereBetween, but matches the value should be outside of provided range.
whereNull / orWhereNull
whereNotNull / orWhereNotNull
Same as whereNull / orWhereNull, but matches the value should not be null.
whereHas / orWhereHas
Queries existance of a relation. This requires your relation to be added to the allowedApiRelations
array on your model. Otherwise it will just ignore this query.
Simple existence check, will only return results that has any bookings related to it:
Filter the existance check by a column value. Will only return results that has a booking with id 1 related to it:
Advanced querying. Will accept most query methods:
whereDoesntHave / orWhereDoesntHave
Same as whereHas / orWhereHas, but matches the absence of a relation.
whereDate
Query by date. All abbreviations of this method are: whereDate / whereMonth / whereDay / whereYear / whereTime.
You can also do more advanced matching by defining an operator (=
, !=
, >
, <
). When defining an operator you also need to define a value
parameter.
search
This is a method to make it a bit easier to do search queries on multiple columns, instead of doing advanced where
-queries.
Parameters:
select
Limit the data-set to only pull specific colummns.
or
You can also select relation properties, if you've loaded this with with
.
orderBy
Order the result based on one or more columns.
or multiple columns
Define the order with desc
or asc
. There is also a specialized order called localizedDesc
and localizedDesc
that will run the ordering with a preconfigured collation based on current locale. Read more about configuration.
You can also order based on a relation property, if you've loaded this with with
.
groupBy
Group the result by a column.
limit
Limit the total possible returned result by a number.
Will only ever return 2 results at most.
with
Eager load relations. This requires your relation to be added to the allowedApiRelations
array on your model (read more). Otherwise it will just ignore this query.
Be cautions of performance of loading a lot of relations. Only do this where you know you will only get a limited result-set.
or
appends
Append attributes. This requires your attribute to be added to the allowedApiAppends
array on your model (read more). Otherwise it will just ignore this query.
Be cautions of performance of loading a lot of appendable attributes. These are processed after the query result on each model. Only do this where you know you will only get a limited result-set and the appended attribute is not hammering the database etc.
or
counts
Relation-counts. This will return a property with an integer indicating the number of results of a relation this model has.
This requires your attribute to be added to the allowedApiCounts
array on your model (read more). Otherwise it will just ignore this query.
Be cautions of performance of counting a lot of relations. They will produce extra database hits.
or
pagination / per_page
Pagination is not specifically handled by the query builder, but here's an example of how you can do this:
Pagination is now true by default, and then to control the pagination for the query, you can now pass extra URL parameters.
To turn pagination off completely:
To adjust the number of results per page
Defining allowed fields, relations, appendable attributes and counts
To avoid exposing everything on your models, you will have to define each relation, appended attribute or count that you want to be queryable.
The validation basically works the same on all of them. The only exception is allowedApiFields
, where there is a default to allow all standard fields. After including the QueryBuilderModelTrait
in your model, you can add the following methods to it:
`
`
All versions of api-query-builder with dependencies
illuminate/support Version ^8.0 | ^9.0 | ^10.0
illuminate/http Version ^8.0 | ^9.0 | ^10.0
illuminate/database Version ^8.0 | ^9.0 | ^10.0