Backend Queries

Enforce parameterized queries and optimize Eloquent and raw SQL database access.

Updated Oct 31, 2025
One-click install
npx skills add https://github.com/FlorianRiquelme/statamic-assets --skill backend-queries-florianriquelme
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: Backend Queries
Source: https://github.com/FlorianRiquelme/statamic-assets/tree/main/.claude/skills/backend-queries
Command: npx skills add https://github.com/FlorianRiquelme/statamic-assets --skill backend-queries-florianriquelme

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

This Skill ensures your database queries are efficient, secure, and performant, preventing common issues like N+1 problems, SQL injection vulnerabilities, and slow application responses.

Core Features & Use Cases

  • Performance: Utilizes eager loading to prevent N+1 issues, selective column fetching, and strategic indexing for faster data retrieval.
  • Security: Employs parameterized queries and transactions to prevent injection attacks and ensure data consistency.
  • Use Case: When fetching a list of users with their associated posts, this skill ensures eager loading is used to avoid N+1 queries, significantly speeding up the page load and reducing database load.

Quick Start

Refactor the attached Eloquent query to eager load related 'comments' and select only the 'id' and 'title' columns from the 'posts' table.

Frequently Asked Questions about Backend Queries

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I prevent N+1 queries when fetching related data in Eloquent?

N+1 queries occur when loading related records one-by-one instead of together. Use Eloquent's eager loading with the `with()` method to fetch all related data in a single query, dramatically reducing database calls and improving performance.

What's the best way to protect database queries from SQL injection?

SQL injection attacks exploit unsanitized user input in queries. Use parameterized queries, which separate data from SQL logic—Eloquent's query builder and prepared statements handle this automatically when you avoid raw string concatenation.

How do I optimize slow database queries in my backend?

Optimize queries by using selective column fetching to retrieve only needed data, adding database indexes on frequently queried columns, implementing caching for expensive queries, and using transactions to ensure data consistency without blocking.

Can I use query optimization techniques with raw SQL and Eloquent together?

Yes. Both raw SQL queries and Eloquent's query builder support parameterized queries, eager loading patterns, selective column selection, indexing strategies, and query timeouts, allowing consistent optimization across your database access layer.

When should I use query caching instead of database indexing?

Caching suits frequently-run queries with expensive computation or large result sets that rarely change. Indexing optimizes how the database retrieves data physically. Use both together: index for fast lookups, cache for repeatedly-accessed expensive results.

How do transactions prevent data inconsistency in concurrent database operations?

Transactions group multiple queries into atomic units—either all succeed or all fail. This prevents partial updates when concurrent requests modify related data, ensuring your database remains in a consistent state even under high load.