Index of every page on this site: llms.txt
All skills
laravel avatar

/laravel-best-practices

@6004f09
by laravellaravel/boost3.7k stars
390
Run it yourself

Apply this skill whenever writing, reviewing, or refactoring Laravel PHP code. This includes creating or modifying controllers, models, migrations, form requests, policies, jobs, scheduled commands, service classes, and Eloquent queries. Triggers for N+1 and query performance issues, caching strategies, authorization and security patterns, validation, error handling, queue and job configuration, route definitions, and architectural decisions. Also use for Laravel code reviews and refactoring existing Laravel code to follow best practices. Covers any task involving Laravel backend PHP code patterns.

Use this Skill: https://skilld.dev/gh/laravel/boost/laravel-best-practices

Nothing lands on disk. Nothing to clean up.

Fork this Skill

Edit a local copy. It keeps the author and licence.

rulescollections.md

≈607 tokens on demand. Your agent reads this file only when SKILL.md points to it.

Collection best practices

Use higher-order messages for simple operations

Explicit closure:

$users->each(function (User $user) {
    $user->markAsVip();
});

Concise equivalent:

$users->each->markAsVip();

Higher-order messages work with supported collection methods such as each, map, filter, and sum. Use an explicit closure when arguments or nontrivial logic would be clearer.

Choose between cursor() and lazy()

cursor() executes one query and hydrates one model at a time, but it cannot eager load relationships. The database driver's result buffering can still use a lot of memory for very large results. Use it for low-memory, attribute-only iteration when one long-running query is acceptable.

lazy() executes multiple chunked queries and returns a flat LazyCollection. It supports eager loading relationships for each chunk and avoids holding one database cursor open for the entire iteration.

With relationships:

User::with('roles')->lazy()->each(function (User $user) {
    // The roles for this chunk have been eager loaded.
});

Without relationships:

User::cursor()->each(function (User $user) {
    // Process model attributes.
});

Use lazyById() when updating records while iterating

lazy() uses offset pagination, so updating a column that affects the query can shift rows and make the iteration skip records or process them twice. lazyById() paginates by a monotonic key and is safer when you update other columns during iteration. Do not change the pagination key itself while iterating.

Use toQuery() for bulk operations on collections

Use toQuery() to build a query from the models in an Eloquent collection instead of manually constructing a whereIn clause.

Manual query:

User::whereIn('id', $users->modelKeys())->update(['active' => false]);

Collection query:

$users->toQuery()->update(['active' => false]);

toQuery() requires a non-empty Eloquent collection whose models are of the same type. Like other bulk Eloquent updates, it does not dispatch per-model update events, so use it only when you don't need those events.

Use #[CollectedBy] for custom collection classes

The #[CollectedBy] attribute declares the custom collection class without requiring a newCollection() override.

#[CollectedBy(UserCollection::class)]
class User extends Model {}

Source: SKILL.md on GitHub

skilld matched fixed text patterns in SKILL.md and file names. Patterns miss obfuscated code.

skilld run checks every file with the same patterns. It asks for approval before it loads a Skill with a behavior marked Needs approval.

No alerts1mo3 checks · Risk SAFE
  • Gen Agent Trust Hub1mo

    The skill provides a comprehensive set of best practices for Laravel development, including specific guidance on security, performance, and architecture. No malicious patterns or security risks were identified.

  • Socket1mo

    No alerts

  • Snyk1mo

    Risk: LOW · No issues

Signed by skilld at 6004f09. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub last week.

Activeupdated last month
metadata
{
  "author": "laravel"
}

README badge

README badge for laravel/boost/laravel-best-practices