⌘K
Noting Found

View all related articles

Simple way to change the value of a variable within a closer function

Snippets 1 min read

If you want to change a value of a variable within a closer function, use the & before each variable.

1 
2$roles = [];
3 
4$users->each(function ($user) use ($roles) {
5 $roles = $user->roles;
6});

Output

1// [] empty array

Use the & before the variable.

1$users->each(function ($user) use ($roles) {
2 $roles = $user->roles;
3});

Output

1// array of rules
Related Tags

About the Author

Oussama's Profile Picture
Oussama
Full Stack Web Developer | Technical Writer

Oussama is an experienced full-stack web developer with a strong focus on Laravel. He's passionate about crafting web applications with Filament and the TALL Stack. With 8+ years of experience, and he's a dedicated open-source contributor.

Comments

Join our newsletter

Subscribe to Our Newsletter and never miss our offers, latest news, Articles, etc.

We care about the protection of your data. Read our Privacy Policy.