Welcome to Laranepal - Nepal's Laravel Community!

Unflattening Arrays With Laravel Collections: Undot

Published on February 12, 2024 by

Unflattening Arrays with Laravel Collections: Undot

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.email' => '[email protected]',
'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",
"email" => "[email protected]",
"social" => [
"twitter" => '@john_doe',
"github" => 'JohnDoe',
],
],
]

For more read officle page: Collections - Laravel 10.x - The PHP Framework For Web Artisans Thank you.

Dinesh Uprety

Senior Software Engineer • Writer @ Laranepal • PHP, Laravel, Livewire, TailwindCSS & VueJS • CEO @ Laranepal & Founder @ laracodesnap

Filed in:

Discussion

Login or register to comment or ask questions

No comments yet

Be the first to share your thoughts or ask a question.

Join the conversation

Sign in to share your thoughts with the community.