ModelQuery<T extends Model> class

A type-safe query builder that returns model instances instead of raw maps.

Wraps the underlying QueryBuilder and automatically maps results to T.

final users = await ModelQuery<User>(db, User.fromRow)
  .where('active', '=', true)
  .orderBy('name')
  .get();

Constructors

ModelQuery(DatabaseExecutor _executor, ModelFactory<T> _factory, {String? tableName})
Creates a typed query for models of type T.

Properties

hashCode int
The hash code for this object.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

count() Future<int>
Returns the count of matching rows.
create(T model) Future<T>
Creates a new record from the model and returns the model with id populated.
deleteAll() Future<int>
Deletes all rows matching the current query.
destroy(dynamic id) Future<void>
Deletes a model by its primary key.
destroyModel(T model) Future<void>
Deletes a model instance from the database.
exists() Future<bool>
Checks if any rows match the current query.
find(dynamic id) Future<T?>
Finds a model by its primary key id.
findOrFail(dynamic id) Future<T>
Finds a model by primary key or throws if not found.
first() Future<T?>
Returns the first matching model, or null if none found.
get() Future<List<T>>
Executes the query and returns all matching models.
groupBy(String column) ModelQuery<T>
Adds a GROUP BY clause.
having(String column, String operator, dynamic value) ModelQuery<T>
Adds a HAVING clause.
join(String table, String col1, String op, String col2) ModelQuery<T>
Adds an INNER JOIN.
leftJoin(String table, String col1, String op, String col2) ModelQuery<T>
Adds a LEFT JOIN.
limit(int value) ModelQuery<T>
Adds a LIMIT clause.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
offset(int value) ModelQuery<T>
Adds an OFFSET clause.
orderBy(String column, [String direction = 'ASC']) ModelQuery<T>
Adds an ORDER BY clause.
orWhere(String column, String operator, dynamic value) ModelQuery<T>
Adds an OR WHERE clause.
paginate({int page = 1, int perPage = 15}) Future<PaginatedResult<T>>
Returns a paginated result.
save(T model) Future<T>
Updates an existing model in the database.
select(List<String> columns) ModelQuery<T>
Specifies which columns to select.
toString() String
A string representation of this object.
inherited
updateAll(Map<String, dynamic> data) Future<int>
Performs a bulk update on all rows matching the current query.
where(String column, String operator, dynamic value) ModelQuery<T>
Adds a WHERE clause.
whereIn(String column, List values) ModelQuery<T>
Adds a WHERE column IN (...) clause.
whereNotNull(String column) ModelQuery<T>
Adds a WHERE column IS NOT NULL clause.
whereNull(String column) ModelQuery<T>
Adds a WHERE column IS NULL clause.

Operators

operator ==(Object other) bool
The equality operator.
inherited