Token导航 LogoToken导航TokenDH.com
开发规范external-servicegithub未标认证来源可访问许可证需确认审计通过

laravel-best-practicesLaravel 最佳实践

Agent Skill

laravel-best-practices 用于记录任务执行中的错误、用户纠正、经验和能力缺口,适合在 Codex、Claude、Cursor、Gemini CLI 中希望让 Agent 持续沉淀问题、修正和最佳实践时使用。可结合来源仓库、安装命令和原始 README 继续核验具体用法。安装前建议确认权限范围、维护状态,以及是否会触发联网、命令执行或文件读写。

总安装

3,781

周安装

156

GitHub Stars

3,423

下载量

1,236
CodexClaudeCursorGemini CLI

安装说明

本站只整理中文说明和来源信息,不托管安装包,也不代用户安装。

GitHub

来源数

2

许可证

unknown

最后核验

2026-05-01

来源状态

来源可访问

安装方式

通过对话安装

复制提示词发给支持本地命令或 Skills 的 AI 助手,先确认命令和权限,再让它执行。

请帮我安装这个 Agent Skill:laravel-best-practices(Laravel 最佳实践)
来源仓库:https://github.com/laravel/boost
仓库路径:skills/laravel-best-practices
安装命令:
npx skills add https://github.com/laravel/boost --skill laravel-best-practices
安装前请先检查当前环境是否支持对应 CLI,并向我确认将要执行的命令、安装目录、联网范围和文件读写权限;确认后再执行。

命令行安装

复制命令到本机终端执行。该命令会通过 npx skills 从第三方来源获取 Skill;本站只展示命令,不托管安装包,也不自动执行。

skills.shnpx skills
npx skills add https://github.com/laravel/boost --skill laravel-best-practices

简介

该技能聚焦于 Laravel 应用开发的规范与优化建议, 帮助开发者遵循行业标准提升代码质量。laravel-best-practices 属于开发规范类 Skill,可作为该场景下的辅助能力补充。

  • 核心能力涵盖路由设计、模型关系、查询构建等关键模块的最佳实现方式。
  • 通过 GitHub 安装并集成到支持技能扩展的 AI 编程工具中。
  • 使用前请确认宿主平台是否支持外部技能加载及网络访问权限。

SKILL.md

Laravel Best Practices

Best practices for Laravel, prioritized by impact. Each rule teaches what to do and why. For exact API syntax, verify with search-docs.

Consistency First

Before applying any rule, check what the application already does. Laravel offers multiple valid approaches — the best choice is the one the codebase already uses, even if another pattern would be theoretically better. Inconsistency is worse than a suboptimal pattern.

Check sibling files, related controllers, models, or tests for established patterns. If one exists, follow it — don't introduce a second way. These rules are defaults for when no pattern exists yet, not overrides.

Quick Reference

1. Database Performance → rules/db-performance.md

  • Eager load with with() to prevent N+1 queries
  • Enable Model::preventLazyLoading() in development
  • Select only needed columns, avoid SELECT *
  • chunk() / chunkById() for large datasets
  • Index columns used in WHERE, ORDER BY, JOIN
  • withCount() instead of loading relations to count
  • cursor() for memory-efficient read-only iteration
  • Never query in Blade templates

2. Advanced Query Patterns → rules/advanced-queries.md

  • addSelect() subqueries over eager-loading entire has-many for a single value
  • Dynamic relationships via subquery FK + belongsTo
  • Conditional aggregates (CASE WHEN in selectRaw) over multiple count queries
  • setRelation() to prevent circular N+1 queries
  • whereIn + pluck() over whereHas for better index usage
  • Two simple queries can beat one complex query
  • Compound indexes matching orderBy column order
  • Correlated subqueries in orderBy for has-many sorting (avoid joins)

3. Security → rules/security.md

  • Define $fillable or $guarded on every model, authorize every action via policies or gates
  • No raw SQL with user input — use Eloquent or query builder
  • {{}} for output escaping, @csrf on all POST/PUT/DELETE forms, throttle on auth and API routes
  • Validate MIME type, extension, and size for file uploads
  • Never commit .env, use config() for secrets, encrypted cast for sensitive DB fields

4. Caching → rules/caching.md

  • Cache::remember() over manual get/put
  • Cache::flexible() for stale-while-revalidate on high-traffic data
  • Cache::memo() to avoid redundant cache hits within a request
  • Cache tags to invalidate related groups
  • Cache::add() for atomic conditional writes
  • once() to memoize per-request or per-object lifetime
  • Cache::lock() / lockForUpdate() for race conditions
  • Failover cache stores in production

5. Eloquent Patterns → rules/eloquent.md

  • Correct relationship types with return type hints
  • Local scopes for reusable query constraints
  • Global scopes sparingly — document their existence
  • Attribute casts in the casts() method
  • Cast date columns, use Carbon instances in templates
  • whereBelongsTo($model) for cleaner queries
  • Never hardcode table names — use (new Model)->getTable() or Eloquent queries

6. Validation & Forms → rules/validation.md

  • Form Request classes, not inline validation
  • Array notation ['required', 'email'] for new code; follow existing convention
  • $request->validated() only — never $request->all()
  • Rule::when() for conditional validation
  • after() instead of withValidator()

7. Configuration → rules/config.md

  • env() only inside config files
  • App::environment() or app()->isProduction()
  • Config, lang files, and constants over hardcoded text

8. Testing Patterns → rules/testing.md

  • LazilyRefreshDatabase over RefreshDatabase for speed
  • assertModelExists() over raw assertDatabaseHas()
  • Factory states and sequences over manual overrides
  • Use fakes (Event::fake(), Exceptions::fake(), etc.) — but always after factory setup, not before
  • recycle() to share relationship instances across factories

9. Queue & Job Patterns → rules/queue-jobs.md

  • retry_after must exceed job timeout; use exponential backoff [1, 5, 10]
  • ShouldBeUnique to prevent duplicates; ShouldBeUniqueUntilProcessing for early lock release
  • Always implement failed(); with retryUntil(), set $tries = 0
  • RateLimited middleware for external API calls; Bus::batch() for related jobs
  • Horizon for complex multi-queue scenarios

10. Routing & Controllers → rules/routing.md

  • Implicit route model binding
  • Scoped bindings for nested resources
  • Route::resource() or apiResource()
  • Methods under 10 lines — extract to actions/services
  • Type-hint Form Requests for auto-validation

11. HTTP Client → rules/http-client.md

  • Explicit timeout and connectTimeout on every request
  • retry() with exponential backoff for external APIs
  • Check response status or use throw()
  • Http::pool() for concurrent independent requests
  • Http::fake() and preventStrayRequests() in tests

12. Events, Notifications & Mail → rules/events-notifications.md, rules/mail.md

  • Event discovery over manual registration; event:cache in production
  • ShouldDispatchAfterCommit / afterCommit() inside transactions
  • Queue notifications and mailables with ShouldQueue
  • On-demand notifications for non-user recipients
  • HasLocalePreference on notifiable models
  • assertQueued() not assertSent() for queued mailables
  • Markdown mailables for transactional emails

13. Error Handling → rules/error-handling.md

  • report()/render() on exception classes or in bootstrap/app.php — follow existing pattern
  • ShouldntReport for exceptions that should never log
  • Throttle high-volume exceptions to protect log sinks
  • dontReportDuplicates() for multi-catch scenarios
  • Force JSON rendering for API routes
  • Structured context via context() on exception classes

14. Task Scheduling → rules/scheduling.md

  • withoutOverlapping() on variable-duration tasks
  • onOneServer() on multi-server deployments
  • runInBackground() for concurrent long tasks
  • environments() to restrict to appropriate environments
  • takeUntilTimeout() for time-bounded processing
  • Schedule groups for shared configuration

15. Architecture → rules/architecture.md

  • Single-purpose Action classes; dependency injection over app() helper
  • Prefer official Laravel packages and follow conventions, don't override defaults
  • Default to ORDER BY id DESC or created_at DESC; mb_* for UTF-8 safety
  • defer() for post-response work; Context for request-scoped data; Concurrency::run() for parallel execution

16. Migrations → rules/migrations.md

  • Generate migrations with php artisan make:migration
  • constrained() for foreign keys
  • Never modify migrations that have run in production
  • Add indexes in the migration, not as an afterthought
  • Mirror column defaults in model $attributes
  • Reversible down() by default; forward-fix migrations for intentionally irreversible changes
  • One concern per migration — never mix DDL and DML

17. Collections → rules/collections.md

  • Higher-order messages for simple collection operations
  • cursor() vs. lazy() — choose based on relationship needs
  • lazyById() when updating records while iterating
  • toQuery() for bulk operations on collections

18. Blade & Views → rules/blade-views.md

  • $attributes->merge() in component templates
  • Blade components over @include; @pushOnce for per-component scripts
  • View Composers for shared view data
  • @aware for deeply nested component props

19. Conventions & Style → rules/style.md

  • Follow Laravel naming conventions for all entities
  • Prefer Laravel helpers (Str, Arr, Number, Uri, Str::of(), $request->string()) over raw PHP functions
  • No JS/CSS in Blade, no HTML in PHP classes
  • Code should be readable; comments only for config files

How to Apply

Always use a sub-agent to read rule files and explore this skill's content.

  1. Identify the file type and select relevant sections (e.g., migration → §16, controller → §1, §3, §5, §6, §10)
  2. Check sibling files for existing patterns — follow those first per Consistency First
  3. Verify API syntax with search-docs for the installed Laravel version

适合场景

01

用户想查找某类 Agent Skill 时

02

需要根据任务场景推荐可安装能力包时

03

需要对比不同来源的安装命令和来源信息时

能力概览

能力 1

按任务关键词查找相关 Skills

能力 2

展示可复制的安装命令

能力 3

保留来源站点、仓库和原始说明,方便继续核验

能力 4

展示第三方安全扫描或审计结果

安装后应在对应宿主中按原始 README 的触发条件使用;具体调用方式请以来源页面和 README 为准。

平台分布

Codex

35%
按下载量换算433

Claude

29.94%
按下载量换算370

Cursor

20.5%
按下载量换算253

Gemini CLI

8.77%
按下载量换算108

安全审计

Gen Agent Trust Hub

通过

Socket

通过

Snyk

通过

权限和风险

external-service

该 Skill 可能调用第三方服务、云服务或外部模型 API,使用前需要确认账号、额度、数据发送范围和服务条款。

安装前确认

本站仅展示第三方公开信息,不托管安装包,不提供自动安装或运行环境。安装前应自行审查源码、依赖和命令行为。当前只有一个来源,正式发布前建议补源仓库或其他目录站核验。

来源信息

继续浏览同类 Skills