← Back to Web app developers
See how it helps
Eloquent models and queries
Designs expressive Laravel queries and model relationships with predictable events, memory use and data conversion.
laravel-eloquent
Team members using this skill
When is it useful?
Use when a feature loads too much data or model behavior hides unexpected queries and writes.
What does it help you do?
- Inspects casts, observers and mass-assignment rules before altering persistence.
- Groups conditions correctly so alternatives cannot escape tenant filters.
- Uses database aggregation and bounded iteration instead of loading entire collections.
Try asking this
Refactor our order report query with scoped filters and database totals, preserving model events where individual updates require them. Combine an OR search with a tenant filter and compare report totals. No matching record from another tenant may escape through the alternate condition.
Detailed documentation
Read the skill’s original instructions, examples and checks in their authored language.
Show less
Eloquent & Query Design
Models, relationships and casts
- Declare explicit return types on relationships (
HasMany,BelongsTo,BelongsToMany,HasOne,MorphTo) and prefer Eloquent relationships and query builder methods (Model::query()) over raw SQL or manual joins. - Inspect a model's casts (
casts()method or$castsproperty, following the repo),$fillable/$guarded, global scopes, observers and model events before changing how it is created, updated or deleted. - Configure casts for dates (
immutable_datetime), PHP enums, booleans, JSON (array/AsArrayObject) and encrypted attributes. - Never hide external network calls, writes or heavy queries inside an accessor; accessors may run on every serialization.
- Remember that bulk queries (
Model::where(...)->update(),delete(),insert(),upsert()) bypass Eloquent model events and observers.
Scopes and expressive queries
- Encapsulate reusable query constraints in local scopes (
#[Scope]orscopeActive(Builder $query): Builder). GrouporWhereclauses inside a closure ($query->where(fn ($q) => $q->where(...)->orWhere(...))) so precedence never leaks past tenant or status filters. - Push filtering, counting and aggregation into SQL (
withCount,withSum,withExists,whereRelation,whereDoesntHave,addSelectsubqueries, orselectRawwith bound parameters) instead of calling->get()and filtering or summing in PHP Collections. - Stream or batch large datasets with
chunkById(),lazy()orcursor()rather than loading thousands of models into memory withall()orget().
Included files 2
LICENSE.txtSKILL.md
