ModelQuery<T extends Model> class
final
Model-aware query (Laravel's Eloquent Builder): wraps a QueryBuilder
and hydrates models, applies global scopes and soft deletes, eager loads
relations and offers whereHas / withCount.
Constructors
-
ModelQuery(ModelDefinition<
T> definition, {DatabaseManager? db})
Properties
- builder → QueryBuilder
-
The underlying builder (column-level operations delegate to it).
final
- database → DatabaseManager
-
no setter
-
definition
→ ModelDefinition<
T> -
final
- hashCode → int
-
The hash code for this object.
no setterinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
- tableName → String
-
The name this query's table goes by: definition's table, or the
alias of a self-relation subquery.
no setter
Methods
-
addSelect(
List< Object> columns) → ModelQuery<T> -
aliasTable(
String alias) → ModelQuery< T> -
Gives the table an
alias(FROM categories AS laravel_reserved_0), used by relation subqueries on the parent's own table. Soft deletes and key lookups are qualified with it. -
avg(
Object column) → Future< num?> -
chunk(
int size, FutureOr< Object?> callback(ModelCollection<T> models, int page)) → Future<void> -
chunkById(
int size, FutureOr< Object?> callback(ModelCollection<T> models)) → Future<void> -
count(
[Object column = '*']) → Future< int> -
create(
[Map< String, Object?> attributes = const {}]) → Future<T> - Creates and saves a model.
-
createOrFirst(
Map< String, Object?> attributes, [Map<String, Object?> values = const {}]) → Future<T> -
Creates the row; when a unique index already holds it (another
request won the race), returns the row matching
attributes(Laravel'screateOrFirst). Inside a transaction the insert runs in a savepoint, so a refused one leaves the transaction usable (PostgreSQL aborts it otherwise). -
cursorPaginate(
{int? perPage, Cursor? cursor, String? path, String cursorName = 'cursor', Map< String, String> query = const {}}) → Future<CursorPaginator< T> > -
decrement(
String column, [num amount = 1]) → Future< int> -
delete(
) → Future< int> - Bulk delete (soft delete when configured).
-
distinct(
) → ModelQuery< T> -
doesntExist(
) → Future< bool> -
doesntHave(
String relation) → ModelQuery< T> -
exists(
) → Future< bool> -
find(
Object id) → Future< T?> -
findMany(
List< Object?> ids) → Future<ModelCollection< T> > -
findOrFail(
Object id) → Future< T> -
first(
) → Future< T?> -
firstOrCreate(
Map< String, Object?> attributes, [Map<String, Object?> values = const {}]) → Future<T> -
The row matching
attributes, or one created withvaluestoo; when another request creates it first, that row (through createOrFirst, as Laravel 12). -
firstOrFail(
) → Future< T> -
firstOrNew(
Map< String, Object?> attributes, [Map<String, Object?> values = const {}]) → Future<T> -
forceDelete(
) → Future< int> -
forPage(
int page, [int? perPage]) → ModelQuery< T> -
get(
) → Future< ModelCollection< T> > -
groupBy(
List< Object> columns) → ModelQuery<T> -
has(
String relation) → ModelQuery< T> -
having(
Object column, String operator, Object? value) → ModelQuery< T> -
increment(
String column, [num amount = 1]) → Future< int> -
inRandomOrder(
) → ModelQuery< T> -
join(
String table, Object first, [String? operator, String? second]) → ModelQuery< T> -
latest(
[String? column]) → ModelQuery< T> -
lazy(
[int size = 500]) → Stream< T> -
leftJoin(
String table, Object first, [String? operator, String? second]) → ModelQuery< T> -
limit(
int value) → ModelQuery< T> -
loadRelationsOn(
List< Model> models, Map<String, RelationConstraint?> wants) → Future<void> -
Eager loads
wants(possibly nesteda.b.c) ontomodels. -
lockForUpdate(
) → ModelQuery< T> -
make(
[Map< String, Object?> attributes = const {}]) → T - Creates without saving.
-
max(
Object column) → Future< Object?> -
min(
Object column) → Future< Object?> -
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
offset(
int value) → ModelQuery< T> -
oldest(
[String? column]) → ModelQuery< T> -
onlyTrashed(
) → ModelQuery< T> -
orderBy(
Object column, [String direction = 'asc']) → ModelQuery< T> -
orderByDesc(
Object column) → ModelQuery< T> -
orderByRaw(
String sql, [List< Object?> bindings = const []]) → ModelQuery<T> -
Orders by raw SQL:
sqlis used as written, so values go inbindings. -
orWhere(
Object column, [Object? operatorOrValue, Object? value]) → ModelQuery< T> -
orWhereAfterToday(
Object columns) → ModelQuery< T> -
orWhereBeforeToday(
Object columns) → ModelQuery< T> -
orWhereDoesntHave(
String relation, [RelationConstraint? constraint]) → ModelQuery< T> -
orWhereDoesntHaveMorph(
String relation, List< ModelDefinition< types, [MorphConstraint? constraint]) → ModelQuery<Model> >T> -
orWhereFuture(
Object columns) → ModelQuery< T> -
orWhereHas(
String relation, [RelationConstraint? constraint]) → ModelQuery< T> -
orWhereHasMorph(
String relation, List< ModelDefinition< types, [MorphConstraint? constraint]) → ModelQuery<Model> >T> -
orWhereNowOrFuture(
Object columns) → ModelQuery< T> -
orWhereNowOrPast(
Object columns) → ModelQuery< T> -
orWherePast(
Object columns) → ModelQuery< T> -
orWhereToday(
Object columns) → ModelQuery< T> -
orWhereTodayOrAfter(
Object columns) → ModelQuery< T> -
orWhereTodayOrBefore(
Object columns) → ModelQuery< T> -
paginate(
{int? page, int? perPage, String? path, String pageName = 'page', Map< String, String> query = const {}}) → Future<LengthAwarePaginator< T> > -
pluck(
String column, [String? key]) → Future< Object> -
prepared(
) → QueryBuilder - The builder with global scopes, soft deletes and withCount applied.
-
restore(
) → Future< int> -
scope(
String name, [List< Object?> args = const []]) → ModelQuery<T> -
Applies the local scope
name(scopes:in the definition). -
select(
List< Object> columns) → ModelQuery<T> -
selectRaw(
String sql, [List< Object?> bindings = const []]) → ModelQuery<T> -
simplePaginate(
{int? page, int? perPage, String? path, String pageName = 'page', Map< String, String> query = const {}}) → Future<SimplePaginator< T> > -
skip(
int value) → ModelQuery< T> -
sole(
) → Future< T> -
sum(
Object column) → Future< num?> -
take(
int value) → ModelQuery< T> -
tap(
void callback(ModelQuery< T> query)) → ModelQuery<T> -
toSql(
) → String -
toString(
) → String -
A string representation of this object.
inherited
-
update(
Map< String, Object?> values) → Future<int> -
Bulk update (adds
updated_at). -
updateOrCreate(
Map< String, Object?> attributes, Map<String, Object?> values) → Future<T> -
The row matching
attributesupdated withvalues, or created with both; safe against a concurrent create (Laravel 12'supdateOrCreate). -
when(
bool condition, void callback(ModelQuery< T> query), [void otherwise(ModelQuery<T> query)?]) → ModelQuery<T> -
where(
Object column, [Object? operatorOrValue, Object? value]) → ModelQuery< T> -
whereAfterToday(
Object columns) → ModelQuery< T> -
whereAny(
List< String> columns, String operator, Object? value) → ModelQuery<T> -
whereBeforeToday(
Object columns) → ModelQuery< T> -
whereBetween(
String column, Object? from, Object? to) → ModelQuery< T> -
whereColumn(
String first, String operator, String second) → ModelQuery< T> -
whereDate(
String column, String operatorOrValue, [Object? value]) → ModelQuery< T> -
whereDay(
String column, Object operatorOrValue, [Object? value]) → ModelQuery< T> -
whereDoesntHave(
String relation, [RelationConstraint? constraint]) → ModelQuery< T> -
whereDoesntHaveMorph(
String relation, List< ModelDefinition< types, [MorphConstraint? constraint]) → ModelQuery<Model> >T> -
Rows whose
MorphToowner is one oftypesbut does not matchconstraint(or no longer exists). -
whereFuture(
Object columns) → ModelQuery< T> -
whereHas(
String relation, [RelationConstraint? constraint, String boolean = 'AND']) → ModelQuery< T> -
Filters on the existence of related rows.
relationmay be nested (posts.comments): each level becomes anEXISTSinside the previous one, andconstraintapplies to the last. -
whereHasMorph(
String relation, List< ModelDefinition< types, [MorphConstraint? constraint]) → ModelQuery<Model> >T> -
Filters on a
MorphTorelation whose owner is one oftypesand matchesconstraint(called with each type). -
whereIn(
String column, Object values) → ModelQuery< T> -
whereKey(
Object? key) → ModelQuery< T> -
whereKeyNot(
Object? key) → ModelQuery< T> -
whereLike(
String column, String pattern, {bool caseSensitive = false}) → ModelQuery< T> -
whereMonth(
String column, Object operatorOrValue, [Object? value]) → ModelQuery< T> -
whereNotIn(
String column, Object values) → ModelQuery< T> -
whereNotNull(
String column) → ModelQuery< T> -
whereNowOrFuture(
Object columns) → ModelQuery< T> -
whereNowOrPast(
Object columns) → ModelQuery< T> -
whereNull(
String column) → ModelQuery< T> -
wherePast(
Object columns) → ModelQuery< T> -
whereRaw(
String sql, [List< Object?> bindings = const []]) → ModelQuery<T> -
whereToday(
Object columns) → ModelQuery< T> -
whereTodayOrAfter(
Object columns) → ModelQuery< T> -
whereTodayOrBefore(
Object columns) → ModelQuery< T> -
whereYear(
String column, Object operatorOrValue, [Object? value]) → ModelQuery< T> -
with_(
Object relations) → ModelQuery< T> -
Eager loads relations:
with(['posts', 'posts.comments'])orwith({'posts': (q) => q.where('published', true)}). -
withAvg(
Object relations, String column) → ModelQuery< T> -
Adds
<relation>_avg_<column>columns. -
withCount(
Object relations) → ModelQuery< T> -
Adds
<relation>_countcolumns ('posts as drafts_count'names the column). -
withExists(
Object relations) → ModelQuery< T> -
Adds boolean
<relation>_existscolumns. -
withMax(
Object relations, String column) → ModelQuery< T> -
Adds
<relation>_max_<column>columns. -
withMin(
Object relations, String column) → ModelQuery< T> -
Adds
<relation>_min_<column>columns. -
withoutGlobalScope(
String name) → ModelQuery< T> -
withoutGlobalScopes(
[List< String> ? names]) → ModelQuery<T> -
Without every global scope, soft deletes included; with
names, only those ofnamedGlobalScopes. -
withoutTrashed(
) → ModelQuery< T> -
withSum(
Object relations, String column) → ModelQuery< T> -
Adds
<relation>_sum_<column>columns. -
withTrashed(
) → ModelQuery< T> -
withWhereHas(
String relation, [RelationConstraint? constraint]) → ModelQuery< T> -
whereHas+withwith the same constraint.
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited
Static Methods
-
parseWith(
Object relations) → Map< String, RelationConstraint?> -
Parses
with()arguments intoname → constraintpairs.