Query Builder
Query Builder provides a user-friendly interface where users can define the criteria and parameters for database queries without needing to write the SQL code manually. Query builders are commonly used in database management systems and data analysis tools to streamline the query creation process, making it more accessible to users who may not be proficient in writing SQL queries.
create
await Actor().create(
{'name': 'John Wick', 'age': 60}
);
insert
await Actor().insert(
{'name': 'John Wick', 'age': 60}
);
await Actor().insertMultiple([
{'name': 'John Wick', 'age': 60},
{'name': 'John Doe', 'age': 25},
]);
update
await Actor()
.where('id', 3)
.where('status', 'active')
.update({
"name": "Updated AJ",
"age": 120,
});
count
await Actor().count();
await Actor().where('age', '>=' , 23).count();
find
await Actor().find(id);
await Actor().find('name', 'John Wick');
getFirst
Actor actor = await Actor().getFirst(); // limit = 1
all
List<Actor> actors = await Actor().all();
for(Actor actor in actors) {
print(actor.id)
}
get
List<Actor> actors = await Actor().where('name', 'John Wick').get();
for(Actor actor in actors) {
print(actor.id)
}
toSql
String query = Actor().where('name', 'John Wick').toSql();
print(query)
delete
await Actor().where('name', 'John Wick').delete();
forceDelete
await Actor().where('name', 'John Wick').forceDelete();
Info
This work only with softDeletes.
withTrash
List actors = await Actor().where('name', 'John Wick').withTrash().get();
for(Actor actor in actors) {
print(actor.id)
}
Info
This work only with softDeletes.
select
await Actor()
.select('id')
.select('name')
.where('name', 'John Wick').get();
await Actor()
.select(['id', 'name', 'age']).where('name', 'John Wick').get();
await Actor()
.select('id, name, age').where('name', 'John Wick').get();
whereIn
await Actor().whereIn('id', ['1', '2']).get();
where
await Actor().where('name', 'John Wick').get();
await Actor().where('name', '=', 'John Wick').get();
await Actor().where('age', '>=', 23).get();
orWhere
await Actor().orWhere('name', 'John Wick').get();
await Actor().orWhere('name', '=', 'John Wick').get();
await Actor().orWhere('age', '>=', 23).get();
await Actor()
.where('name', 'John Doe').orWhere('name', 'John Wick').get();
whereRaw
await Actor().whereRaw('name = @name', {'name', 'John Wick'}).get();
orWhereRaw
await Actor().orWhereRaw('name = @name', {'name', 'John Wick'}).get();
limit
, take
, offset
await Actor().limit(10).get();
await Actor().take(10).get();
await Actor().limit(10).offset(10).get();
Info
Limit and take are the same function.
groupBy
await Actor()
.select('count(*) as total, name').groupBy('name').get();
orderBy
await Actor().orderBy('name').get();
await Actor().orderBy('name', 'desc').get();
await Actor().orderBy('name', 'desc').get();
await Actor()
.orderBy('name', 'asc')
.orderBy('id', 'desc')
.get();
join
await Actor()
.join('actor_info', 'actor_info.admin_id', 'admin.id')
.get();
leftJoin
await Actor()
.leftJoin('actor_info', 'actor_info.admin_id', 'admin.id')
.get();
rightJoin
await Actor()
.rightJoin('actor_info', 'actor_info.admin_id', 'admin.id')
.get();
joinRaw
await Actor()
.joinRaw('actor_info on actor_info.admin_id = admin.id')
.get();
leftJoinRaw
await Actor()
.leftJoinRaw('actor_info on actor_info.admin_id = admin.id')
.get();
rightJoinRaw
await Actor()
.rightJoinRaw('actor_info on actor_info.admin_id = admin.id')
.get();
debug
await Actor().debug(true).all();