← Back to Website builders

Skill · Version 1.0.0

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

See how it helps

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.

Details…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 $casts property, 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] or scopeActive(Builder $query): Builder). Group orWhere clauses 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, addSelect subqueries, or selectRaw with bound parameters) instead of calling ->get() and filtering or summing in PHP Collections.
  • Stream or batch large datasets with chunkById(), lazy() or cursor() rather than loading thousands of models into memory with all() or get().

Included files 2

  • LICENSE.txt
  • SKILL.md