Query Builder Methods
include
- Arguments:
(...args)
- Returns:
self
Eager load relationships.
await Model.include('user', 'category')
Array
Available in version >= v1.8.0
await Model.include(['user', 'category'])
with
is an alias of this method.
append
- Arguments:
(...args)
- Returns:
self
Append attributes.
await Model.append('likes', 'shares')
Array
Available in version >= v1.8.0
await Model.append(['likes', 'shares'])
select
- Arguments:
(...fields)
- Returns:
self
Set the columns to be selected.
Single entity
await Model.select(['title', 'content'])
Related entities
await Post.select({
posts: ['title', 'content'],
user: ['age', 'firstName']
})
where
- Arguments:
(field, value)
- Returns:
self
Add a basic where clause to the query.
await Model.where('status', 'active')
Nested
Available in version >= v1.8.0
await Model.where(['user', 'status'], 'active')
Object
Available in version >= v1.10.0
await Model.where({ user: { status: 'active' } })
whereIn
- Arguments:
(field, array)
- Returns:
self
Add a "where in" clause to the query.
await Model.whereIn('id', [1, 2, 3])
Nested
Available in version >= v1.8.0
await Model.whereIn(['user', 'id'], [1, 2, 3])
Object
Available in version >= v1.10.0
await Model.where({ user: { id: [1, 2, 3] } })
orderBy
- Arguments:
(...args)
- Returns:
self
Add an "order by" clause to the query.
await Model.orderBy('-created_at', 'category_id')
Array
Available in version >= v1.8.0
await Model.orderBy(['-created_at', 'category_id'])
page
- Arguments:
(value)
- Returns:
self
Set the current page.
await Model.page(1)
limit
- Arguments:
(value)
- Returns:
self
Set the page limit.
await Model.limit(20)
params
- Arguments:
(payload)
- Returns:
self
Add custom parameters to the query.
await Model.params({
foo: 'bar',
baz: true
})
GET /resource?foo=bar&baz=true
when
Available in version >= v1.10.0
- Arguments:
(value, callback)
- Returns:
self
Add a conditional clause to the query.
const search = 'foo'
await Model.when(search, (query, value) => query.where('search', value))
custom
- Arguments:
(...args)
- Returns:
self
Build custom endpoints.
await Post.custom('posts/latest')
GET /posts/latest
const user = new User({ id: 1 })
const post = new Post()
await Post.custom(user, post, 'latest')
GET /users/1/posts/latest
config
Available in version >= v1.8.0
- Arguments:
(config)
- Returns:
self
Configuration of HTTP Instance.
await Model.config({
method: 'PATCH',
header: { /* ... */ },
data: { foo: 'bar' }
}).save()
get
- Returns:
Collection | { data: Collection }
Execute the query and get all results.
await Model.get()
all
is an alias of this method.
first
- Returns:
Model | { data: Model }
Execute the query and get the first result.
await Model.first()
find
- Arguments:
(identifier)
- Returns:
Model | { data: Model }
Find a model by its primary key.
await Model.find(1)
$get
- Returns:
Collection
Execute the query and get all results.
await Model.$get()
These
$
-prefixed convenience methods always return the requested content.
They handle and unwrap responses within "data".
$all
is an alias of this method.
$first
- Returns:
Model
Execute the query and get the first result.
await Model.$first()
These
$
-prefixed convenience methods always return the requested content.
They handle and unwrap responses within "data".
$find
- Arguments:
(identifier)
- Returns:
Model
Find a model by its primary key.
await Model.$find(1)
These
$
-prefixed convenience methods always return the requested content.
They handle and unwrap responses within "data".