Menu

Post image 1
Post image 2
1 / 2
0

Laravel Collections Bug Caused by Arrow Functions

DEV Community: productivity·Mahmoud Ramadan·1 day ago
#XjNy2u3j
#dev#hash#fullscreen#forget#function#photo
Reading 0:00
15s threshold

Freek recently shared a post on X about a bug related to Laravel Collections. The issue was surprising to me because the fix looks almost identical to the original code. The code that triggered the issue: collect ([]) -> each ( fn ( string $hash ) => $this -> taggedCache ( $this -> tags ) -> forget ( $hash )); Enter fullscreen mode Exit fullscreen mode After the fix: collect ([]) -> each ( function ( string $hash ) { $this -> taggedCache ( $this -> tags ) -> forget ( $hash ); }); Enter fullscreen mode Exit fullscreen mode At first glance, nothing seems to have changed except for replacing the arrow function with an anonymous function. However, that small difference was enough to cause the bug. Lito highlighted the reason in a comment on the post. The each() method stops iterating when the callback returns false .…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.

Read More