.cursor/skills/laravel-best-practices/rules/collections.md
Incorrect:
$users->each(function (User $user) {
$user->markAsVip();
});
Correct: $users->each->markAsVip();
Works with each, map, sum, filter, reject, contains, etc.
cursor() vs. lazy() Correctlycursor() — 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.
lazyById() When Updating Records While Iteratinglazy() uses offset pagination — updating records during iteration can skip or double-process. lazyById() uses id > last_id, safe against mutation.
toQuery() for Bulk Operations on CollectionsAvoids manual whereIn construction.
Incorrect: User::whereIn('id', $users->pluck('id'))->update([...]);
Correct: $users->toQuery()->update([...]);
#[CollectedBy] for Custom Collection ClassesMore declarative than overriding newCollection().
#[CollectedBy(UserCollection::class)]
class User extends Model {}