Files
SealShare/.claude/skills/laravel-best-practices/rules/collections.md
T
Andreas Reinhold / reiniandClaude Opus 4.8 4391ad7fb9 Regenerate Boost guidelines and skills
Byproduct of the Laravel 13 upgrade: composer's post-update-cmd runs
`artisan boost:update`, and Boost went 2.1.3 to 2.4.x with the update.

Refreshes CLAUDE.md and boost.json for the Laravel 13 / Livewire 4 stack,
updates the livewire, pest and tailwindcss skills, replaces the
developing-with-fortify skill with fortify-development, and adds new
laravel-best-practices and octane-development skills.

Generated output, not hand-edited.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-23 11:17:04 +02:00

1.3 KiB

Collection Best Practices

Use Higher-Order Messages for Simple Operations

Incorrect:

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

Correct: $users->each->markAsVip();

Works with each, map, sum, filter, reject, contains, etc.

Choose cursor() vs. lazy() Correctly

  • cursor() — one model in memory, but cannot eager-load relationships (N+1 risk).
  • lazy() — chunked pagination returning a flat LazyCollection, supports eager loading.

Incorrect: User::with('roles')->cursor() — eager loading silently ignored.

Correct: User::with('roles')->lazy() for relationship access; User::cursor() for attribute-only work.

Use lazyById() When Updating Records While Iterating

lazy() uses offset pagination — updating records during iteration can skip or double-process. lazyById() uses id > last_id, safe against mutation.

Use toQuery() for Bulk Operations on Collections

Avoids manual whereIn construction.

Incorrect: User::whereIn('id', $users->pluck('id'))->update([...]);

Correct: $users->toQuery()->update([...]);

Use #[CollectedBy] for Custom Collection Classes

More declarative than overriding newCollection().

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