Introduction
Laravel Collections are really powerful for working with arrays of data. They provide a fluent, convenient interface for performing common array operations, such as filtering, formatting and transforming data. In this blog post, we'll explore some of the key features of Laravel Collections and how they can be used to simplify and streamline your code.
Creating Collections
The simplest way to create a Collection is to pass an array to the collect() method:
$collection = collect(1, 2, 3, 4, 5);
undot()
This method can be used to transform dot-noted strings into arrays.
Create collection with dot-noted string.
$data = collect([ 'user.first_name' => 'John', 'user.last_name' => 'Doe', 'user.social.twitter' => '@john_doe', 'user.social.github' => 'JohnDoe',]);
Now use undot() method to transform dot-noted strings into arrays
$user = $data->undot();dd($user); //dump data // output [ "user" => [ "first_name" => "John", "last_name" => "Doe", "social" => [ "twitter" => '@john_doe', "github" => 'JohnDoe', ], ],]
For more read officle page: Collections - Laravel 10.x - The PHP Framework For Web Artisans Thank you.
Senior Software Engineer • Writer @ Laranepal • PHP, Laravel, Livewire, TailwindCSS & VueJS • CEO @ Laranepal & Founder @ laracodesnap