How to use the map () method in the Laravel collection
This article mainly introduces how to use the map () method in the Laravel collection, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.
Map ()
The map method is used to traverse the entire collection. It accepts a callback as a parameter. Value and key are passed to the callback. Callbacks can modify values and return them. Finally, a new collection instance of the modified item is returned.
$changed = $collection- > map (function ($value, $key) {$value ['user_id'] + = 1; return $value;}); return $changed- > all ()
Basically, it increases user_id by 1.
The response of the above code is shown below.
[["user_id" = > 2, "title" = > "Helpers in Laravel", "content" = > "Create custom helpers in Laravel", "category" = > "php"], ["user_id" = > 3, "title" = > "Testing in Laravel", "content" = > "Testing File Uploads in Laravel", "category" = > "php"] ["user_id" = > 4, "title" = > "Telegram Bot", "content" = > "Crypto Telegram Bot in Laravel", "category" = > "php"]] Thank you for reading this article carefully. I hope the article "how to use the map () method in the Laravel collection" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support and pay attention to the industry information channel. More related knowledge is waiting for you to learn!