Agent-skills-standard laravel-database-expert
Optimize Laravel queries with subqueries, joinSub, Redis cache-aside patterns, and read/write connection splitting. Use when writing complex joins, implementing Cache::remember with tags, or configuring database read replicas. (triggers: config/database.php, database/migrations/*.php, join, aggregate, subquery, selectRaw, Cache)
install
source · Clone the upstream repo
git clone https://github.com/HoangNguyen0403/agent-skills-standard
Claude Code · Install into ~/.claude/skills/
T=$(mktemp -d) && git clone --depth=1 https://github.com/HoangNguyen0403/agent-skills-standard "$T" && mkdir -p ~/.claude/skills && cp -r "$T/skills/laravel/laravel-database-expert" ~/.claude/skills/hoangnguyen0403-agent-skills-standard-laravel-database-expert && rm -rf "$T"
manifest:
skills/laravel/laravel-database-expert/SKILL.mdsource content
Laravel Database Expert
Priority: P1 (HIGH)
Workflow: Optimize Slow Query
- Profile query — Use
or Laravel Debugbar.DB::enableQueryLog() - Add missing indexes — Create migration for join/where columns.
- Replace N+1 — Use
,withCount()
, orwithSum()
subqueries.addSelect - Cache results — Apply
with tags for frequently accessed data.Cache::remember() - Split reads/writes — Configure
/read
keys inwrite
.config/database.php
Cache-Aside with Tags Example
See implementation examples for cache-aside pattern with tag-based invalidation.
Implementation Guidelines
Advanced Query Builder
- Complex Joins: Prefer
andjoinSub($subquery, 'alias', ...)
over raw SQL orwhereExists(fn($q) => $q->select(DB::raw(1))...)
for correlated subqueries.whereIn - Subqueries: Use
withaddSelect
subquery to avoid N+1 issues.DB::raw - Aggregates: Use
,withCount()
, andwithSum()
directly via Eloquent for optimized column-based aggregation.withAvg() - Raw Expressions: Always use
orselectRaw
with bindings; never use string concatenation in raw queries.whereRaw
Caching Strategy (Redis/Memcached)
- Cache-Aside: Utilize
for frequently accessed data (e.g.,Cache::remember('key', $ttl, $closure)
).posts.all - Redis Tagging: Group related keys using
for grouped invalidation.Cache::tags(['posts', 'user:1']) - Invalidation: Call
to clear specific subsets; never useCache::tags(['posts'])->flush()
globally in production.Cache::flush()
Scalability & Infrastructure
- Read/Write Splitting: Configure 'read' and 'write' keys in
mysql/pgsql connections. Laravel automatically routes SELECT to read and INSERT/UPDATE/DELETE to write; no code changes needed.config/database.php - Indices: Ensure correct database indexes present on all join and aggregate columns.
Anti-Patterns
- No string SQL concatenation: Use bindings or Query Builder.
- No queries in loops: Use subqueries, joins, or aggregates.
- No
: Use tags to target specific cache groups.Cache::flush() - No direct Redis calls: Use Laravel Cache wrappers consistently.