Appearance
QueryBuilder
The QueryBuilder class is a utility class that provides a fluent interface for building queries that can be used to filter, sort, paginate and append relations to the list of entities. It's based on the Laravel Eloquent query builder.
Not all Eloquent methods are supported. The supported methods are listed below.
General methods
getFilter
- Type:
() => Filter - Details Returns the current filter object.
Query building methods
The query builder provides a set of methods to build a query object that can be used to filter, sort, paginate and append relations to the list of entities.
We won't go over what each method does here, but you can find more information in the Laravel Eloquent documentation.
For reference, we'll add the method type signatures here.
The full QueryBuilder class can be found in ui/src/helpers/models/QueryBuilder.ts.
Utility types
typescript
export type ID = string | number
export type Closure = (query: QueryBuilder) => void
export type Operator =
| '='
| '<'
| '>'
| '<='
| '>='
| '<>'
| '!='
| 'like'
| 'not like'
| 'between'
| 'ilike'
| 'not ilike'
export type JoinType = 'inner' | 'left' | 'right' | 'cross'
export type Column = string
export type Relation = string
export type Table = stringwhereKey
typescript
// Add a where clause on the primary key to the query.
whereKey(id: ID): QueryBuilderwhereKeyNot
typescript
// Add a where clause on the primary key to the query.
whereKeyNot(id: ID): QueryBuilderwhere
typescript
// Add a basic where clause to the query.
where(column: Closure): QueryBuilder
where(
column: Column | Column[],
operator: Operator | null,
value: string | number | boolean | null,
boolean: 'and' | 'or',
): QueryBuilderorWhere
typescript
// Add an "or where" clause to the query.
orWhere(
column: Closure | Column | Column[],
operator: Operator | null,
value: string | number | null,
): QueryBuilderwhereNot
typescript
// Add a basic "where not" clause to the query.
whereNot(
column: Closure | Column | Column[],
operator: Operator | null,
value: string | number | null,
boolean: 'and' | 'or',
): QueryBuilderorWhereNot
typescript
// Add an "or where not" clause to the query.
orWhereNot(
column: Closure | Column | Column[],
operator: Operator | null,
value: string | number | null,
): QueryBuilderlatest
typescript
// Add an "order by" clause for a timestamp to the query.
latest(column: Column): QueryBuilderoldest
typescript
// Add an "order by" clause for a timestamp to the query.
oldest(column: Column): QueryBuilderdistinct
typescript
// Force the query to only return distinct results.
distinct(): QueryBuilderwhereColumn
typescript
// Add a "where" clause comparing two columns to the query.
whereColumn(
column: Column | Column[],
operator: Operator | null,
second: string | null,
boolean: string | null,
): QueryBuilderorWhereColumn
typescript
// Add an "or where" clause comparing two columns to the query.
orWhereColumn(
column: Column | Column[],
operator: Operator | null,
second: string | null,
): QueryBuilderwhereIn
typescript
// Add a "where in" clause to the query.
whereIn(column: Column, values: string[] | number[], boolean: string, not: boolean): QueryBuilderorWhereIn
typescript
// Add an "or where in" clause to the query.
orWhereIn(column: Column, values: string[] | number[]): QueryBuilderwhereNotIn
typescript
// Add a "where not in" clause to the query.
whereNotIn(column: Column, values: string[] | number[], boolean: string): QueryBuilderorWhereNotIn
typescript
// Add an "or where not in" clause to the query.
orWhereNotIn(column: Column, values: string[] | number[]): QueryBuilderwhereNull
typescript
// Add a "where null" clause to the query.
whereNull(columns: Column | Column[], boolean: string, not: boolean): QueryBuilderorWhereNull
typescript
// Add an "or where null" clause to the query.
orWhereNull(column: Column): QueryBuilderwhereNotNull
typescript
// Add a "where not null" clause to the query.
whereNotNull(columns: Column | Column[], boolean: string): QueryBuilderwhereBetween
typescript
// Add a where between statement to the query.
whereBetween(
column: Column,
iterable: [string | number, string | number],
boolean: string,
not: boolean,
): QueryBuilderwhereBetweenColumns
typescript
// Add a where between statement using columns to the query.
whereBetweenColumns(
column: Column,
values: [string, string],
boolean: string,
not: boolean,
): QueryBuilderorWhereBetween
typescript
// Add an or where between statement to the query.
orWhereBetween(column: Column, iterable: [string | number, string | number]): QueryBuilderorWhereBetweenColumns
typescript
// Add an or where between statement using columns to the query.
orWhereBetweenColumns(column: Column, values: [string, string]): QueryBuilderwhereNotBetween
typescript
// Add a where not between statement to the query.
whereNotBetween(
column: Column,
iterable: [string | number, string | number],
boolean: string,
): QueryBuilderwhereNotBetweenColumns
typescript
// Add a where not between statement using columns to the query.
whereNotBetweenColumns(column: Column, values: [string, string], boolean: string): QueryBuilderorWhereNotBetween
typescript
// Add an or where not between statement to the query.
orWhereNotBetween(column: Column, iterable: [string | number, string | number]): QueryBuilderorWhereNotBetweenColumns
typescript
// Add an or where not between statement using columns to the query.
orWhereNotBetweenColumns(column: Column, values: [string, string]): QueryBuilderorWhereNotNull
typescript
// Add an "or where not null" clause to the query.
orWhereNotNull(column: Column): QueryBuilderwhereDate
typescript
// Add a "where date" statement to the query.
whereDate(
column: Column,
operator: string | Operator,
value: string | Operator | null,
boolean: string,
): QueryBuilderorWhereDate
typescript
// Add an "or where date" statement to the query.
orWhereDate(
column: Column,
operator: string | Operator,
value: string | Operator | null,
): QueryBuilderwhereTime
typescript
// Add a "where time" statement to the query.
whereTime(
column: Column,
operator: string | Operator,
value: string | Operator | null,
boolean: string,
): QueryBuilderorWhereTime
typescript
// Add an "or where time" statement to the query.
orWhereTime(
column: Column,
operator: string | Operator,
value: string | Operator | null,
): QueryBuilderwhereDay
typescript
// Add a "where day" statement to the query.
whereDay(
column: Column,
operator: string | Operator | null,
value: string | Operator | null,
boolean: string,
): QueryBuilderorWhereDay
typescript
// Add an "or where day" statement to the query.
orWhereDay(
column: Column,
operator: string | Operator | null,
value: string | Operator | null,
): QueryBuilderwhereMonth
typescript
// Add a "where month" statement to the query.
whereMonth(
column: Column,
operator: string | Operator | null,
value: string | Operator | null,
boolean: string,
): QueryBuilderorWhereMonth
typescript
// Add an "or where month" statement to the query.
orWhereMonth(
column: Column,
operator: string | Operator | null,
value: string | Operator | null,
): QueryBuilderwhereYear
typescript
// Add a "where year" statement to the query.
whereYear(
column: Column,
operator: string | Operator | null,
value: string | Operator | null,
boolean: string,
): QueryBuilderorWhereYear
typescript
// Add an "or where year" statement to the query.
orWhereYear(
column: Column,
operator: string | Operator | null,
value: string | Operator | null,
): QueryBuilderwhereExists
typescript
// Add an exists clause to the query.
whereExists(callback: Closure, boolean: string, not: boolean): QueryBuilderorWhereExists
typescript
// Add an or exists clause to the query.
orWhereExists(callback: Closure, not: boolean): QueryBuilderwhereNotExists
typescript
// Add a where not exists clause to the query.
whereNotExists(callback: Closure, boolean: string): QueryBuilderorWhereNotExists
typescript
// Add a where not exists clause to the query.
orWhereNotExists(callback: Closure): QueryBuilderwhereJsonContains
typescript
// Add a "where JSON contains" clause to the query.
whereJsonContains(
column: string,
value: string | number,
boolean: string,
not: boolean,
): QueryBuilderorWhereJsonContains
typescript
// Add an "or where JSON contains" clause to the query.
orWhereJsonContains(column: string, value: string | number): QueryBuilderwhereJsonDoesntContain
typescript
// Add a "where JSON not contains" clause to the query.
whereJsonDoesntContain(column: string, value: string | number, boolean: string): QueryBuilderorWhereJsonDoesntContain
typescript
// Add an "or where JSON not contains" clause to the query.
orWhereJsonDoesntContain(column: string, value: string | number): QueryBuilderwhereJsonContainsKey
typescript
// Add a clause that determines if a JSON path exists to the query.
whereJsonContainsKey(column: string, boolean: string, not: boolean): QueryBuilderorWhereJsonContainsKey
typescript
// Add an "or" clause that determines if a JSON path exists to the query.
orWhereJsonContainsKey(column: string): QueryBuilderwhereJsonDoesntContainKey
typescript
// Add a clause that determines if a JSON path does not exist to the query.
whereJsonDoesntContainKey(column: string, boolean: string): QueryBuilderorWhereJsonDoesntContainKey
typescript
// Add an "or" clause that determines if a JSON path does not exist to the query.
orWhereJsonDoesntContainKey(column: string): QueryBuilderwhereJsonLength
typescript
// Add a "where JSON length" clause to the query.
whereJsonLength(
column: string,
operator: Operator,
value: string | number | null,
boolean: string,
): QueryBuilderorWhereJsonLength
typescript
// Add an "or where JSON length" clause to the query.
orWhereJsonLength(column: string, operator: Operator, value: string | number | null): QueryBuilderwhereFullText
typescript
// Add a "where fulltext" clause to the query.
whereFullText(
columns: Column | Column[],
value: string,
options: string[],
boolean: string,
): QueryBuilderorWhereFullText
typescript
// Add a "or where fulltext" clause to the query.
orWhereFullText(columns: Column | Column[], value: string, options: string[]): QueryBuilderwhereAll
typescript
// Add a "where" clause to the query for multiple columns with "and" conditions between them.
whereAll(
columns: Column[],
operator: Operator | null,
value: string | number | null,
boolean: string,
): QueryBuilderorWhereAll
typescript
// Add an "or where" clause to the query for multiple columns with "and" conditions between them.
orWhereAll(columns: Column[], operator: Operator, value: string | number | null): QueryBuilderwhereAny
typescript
// Add an "where" clause to the query for multiple columns with "or" conditions between them.
whereAny(
columns: Column[],
operator: Operator,
value: string | number | null,
boolean: string,
): QueryBuilderorWhereAny
typescript
// Add an "or where" clause to the query for multiple columns with "or" conditions between them.
orWhereAny(columns: Column[], operator: Operator, value: string | number | null): QueryBuildergroupBy
typescript
// Add a "group by" clause to the query.
groupBy(...groups: Column[]): QueryBuilderhaving
typescript
// Add a "having" clause to the query.
having(
column: Column,
operator: Operator | null,
value: string | number,
boolean: string,
): QueryBuilderorHaving
typescript
// Add an "or having" clause to the query.
orHaving(column: Column, operator: Operator | null, value: string | number): QueryBuilderhavingNested
typescript
// Add a nested having statement to the query.
havingNested(callback: Closure, boolean: string): QueryBuilderhavingNull
typescript
// Add a "having null" clause to the query.
havingNull(columns: Column[], boolean: string, not: boolean): QueryBuilderorHavingNull
typescript
// Add an "or having null" clause to the query.
orHavingNull(column: Column): QueryBuilderhavingNotNull
typescript
// Add a "having not null" clause to the query.
havingNotNull(columns: Column[], boolean: string): QueryBuilderorHavingNotNull
typescript
// Add an "or having not null" clause to the query.
orHavingNotNull(column: Column): QueryBuilderhavingBetween
typescript
// Add a "having between " clause to the query.
havingBetween(
column: Column,
iterable: [string | number, string | number],
boolean: string,
not: boolean,
): QueryBuilderorderBy
typescript
// Add an "order by" clause to the query.
orderBy(column: Column, direction: 'asc' | 'desc'): QueryBuilderorderByDesc
typescript
// Add a descending "order by" clause to the query.
orderByDesc(column: Column): QueryBuilderinRandomOrder
typescript
// Put the query's results in random order.
inRandomOrder(seed: string | number): QueryBuilderskip
typescript
// Alias to set the "offset" value of the query.
skip(value: number): QueryBuilderoffset
typescript
// Set the "offset" value of the query.
offset(value: number): QueryBuildertake
typescript
// Alias to set the "limit" value of the query.
take(value: number): QueryBuilderlimit
typescript
// Set the "limit" value of the query.
limit(value: number): QueryBuilderforPage
typescript
// Set the limit and offset for a given page.
forPage(page: number, perPage: number): QueryBuilderforPageBeforeId
typescript
// Constrain the query to the previous "page" of results before a given ID.
forPageBeforeId(perPage: number, lastId: ID | null, column: Column): QueryBuilderforPageAfterId
typescript
// Constrain the query to the next "page" of results after a given ID.
forPageAfterId(perPage: number, lastId: ID | null, column: Column): QueryBuilderreorder
typescript
// Remove all existing orders and optionally add a new order.
reorder(column: Column | null, direction: 'asc' | 'desc'): QueryBuilderhas
typescript
// Add a relationship count / exists condition to the query.
has(
relation: Relation,
operator: Operator,
count: number,
boolean: string,
callback: Closure | null,
): QueryBuilderhasNested
typescript
// Add nested relationship count / exists conditions to the query.
hasNested(
relations: Relation,
operator: Operator,
count: number,
boolean: string,
callback: Closure | null,
): QueryBuilderorHas
typescript
// Add a relationship count / exists condition to the query with an "or".
orHas(relation: Relation, operator: Operator, count: number): QueryBuilderdoesntHave
typescript
// Add a relationship count / exists condition to the query.
doesntHave(relation: Relation, boolean: string, callback: Closure | null): QueryBuilderorDoesntHave
typescript
// Add a relationship count / exists condition to the query with an "or".
orDoesntHave(relation: Relation): QueryBuilderwhereHas
typescript
// Add a relationship count / exists condition to the query with where clauses.
whereHas(
relation: Relation,
callback: Closure | null,
operator: Operator,
count: number,
): QueryBuilderwithWhereHas
typescript
// Add a relationship count / exists condition to the query with where clauses.
withWhereHas(
relation: Relation,
callback: Closure | null,
operator: Operator,
count: number,
): QueryBuilderorWhereHas
typescript
// Add a relationship count / exists condition to the query with where clauses and an "or".
orWhereHas(
relation: Relation,
callback: Closure | null,
operator: Operator,
count: number,
): QueryBuilderwhereDoesntHave
typescript
// Add a relationship count / exists condition to the query with where clauses.
whereDoesntHave(relation: Relation, callback: Closure | null): QueryBuilderorWhereDoesntHave
typescript
// Add a relationship count / exists condition to the query with where clauses and an "or".
orWhereDoesntHave(relation: Relation, callback: Closure | null): QueryBuilderhasMorph
typescript
// Add a polymorphic relationship count / exists condition to the query.
hasMorph(
relation: Relation,
types: string | string[],
operator: Operator,
count: number,
boolean: string,
callback: Closure | null,
): QueryBuilderorHasMorph
typescript
// Add a polymorphic relationship count / exists condition to the query with an "or".
orHasMorph(
relation: Relation,
types: string | string[],
operator: Operator,
count: number,
): QueryBuilderdoesntHaveMorph
typescript
// Add a polymorphic relationship count / exists condition to the query.
doesntHaveMorph(
relation: Relation,
types: string | string[],
boolean: string,
callback: Closure | null,
): QueryBuilderorDoesntHaveMorph
typescript
// Add a polymorphic relationship count / exists condition to the query with an "or".
orDoesntHaveMorph(relation: Relation, types: string | string[]): QueryBuilderwhereHasMorph
typescript
// Add a polymorphic relationship count / exists condition to the query with where clauses.
whereHasMorph(
relation: Relation,
types: string | string[],
callback: Closure | null,
operator: Operator,
count: number,
): QueryBuilderorWhereHasMorph
typescript
// Add a polymorphic relationship count / exists condition to the query with where clauses and an "or".
orWhereHasMorph(
relation: Relation,
types: string | string[],
callback: Closure | null,
operator: Operator,
count: number,
): QueryBuilderwhereDoesntHaveMorph
typescript
// Add a polymorphic relationship count / exists condition to the query with where clauses.
whereDoesntHaveMorph(
relation: Relation,
types: string | string[],
callback: Closure | null,
): QueryBuilderorWhereDoesntHaveMorph
typescript
// Add a polymorphic relationship count / exists condition to the query with where clauses and an "or".
orWhereDoesntHaveMorph(
relation: Relation,
types: string | string[],
callback: Closure | null,
): QueryBuilderwhereRelation
typescript
// Add a basic where clause to a relationship query.
whereRelation(
relation: Relation,
column: Column,
operator: Operator | null,
value: string | number | null,
): QueryBuilderorWhereRelation
typescript
// Add an "or where" clause to a relationship query.
orWhereRelation(
relation: Relation,
column: Column,
operator: Operator | null,
value: string | number | null,
): QueryBuilderwhereMorphRelation
typescript
// Add a polymorphic relationship condition to the query with a where clause.
whereMorphRelation(
relation: Relation,
types: string | string[],
column: Column,
operator: Operator | null,
value: string | number | null,
): QueryBuilderorWhereMorphRelation
typescript
// Add a polymorphic relationship condition to the query with an "or where" clause.
orWhereMorphRelation(
relation: Relation,
types: string | string[],
column: Column,
operator: Operator | null,
value: string | number | null,
): QueryBuilderwhereMorphedTo
typescript
// Add a morph-to relationship condition to the query.
whereMorphedTo(relation: Relation, model: string, boolean: string): QueryBuilderwhereNotMorphedTo
typescript
// Add a not morph-to relationship condition to the query.
whereNotMorphedTo(relation: Relation, model: string, boolean: string): QueryBuilderorWhereMorphedTo
typescript
// Add a morph-to relationship condition to the query with an "or where" clause.
orWhereMorphedTo(relation: Relation, model: string): QueryBuilderorWhereNotMorphedTo
typescript
// Add a not morph-to relationship condition to the query with an "or where" clause.
orWhereNotMorphedTo(relation: Relation, model: string): QueryBuilder