Vivek Mistry 👋

I’m a Certified Senior Laravel Developer with 8+ years of experience , specializing in building robust APIs and admin panels, frontend templates converting them into fully functional web applications.

Book A Call
  • 15 Jan, 2026
  • 54 Views
  • A clean way to set values in arrays and objects without complex checks.

Modify Nested Data Easily in Laravel Using data_set()

Laravel’s data_set() helper allows developers to set values in deeply nested arrays or objects using dot notation. This article explains how to use it with simple examples covering common real-world scenarios.

Basic Example: Setting a Nested Value

$data = [];
data_set($data, 'user.profile.name', 'Vivek');

Result:

[
    'user' => [
        'profile' => [
            'name' => 'Vivek'
        ]
    ]
]

No checks. No manual nesting.


Updating Existing Data

$user = [
    'profile' => [
        'email' => 'old@mail.com'
    ]
];
data_set($user, 'profile.email', 'new@mail.com');

The value is simply replaced.

Using data_set() with Objects

$user = (object) [];
data_set($user, 'settings.theme', 'dark');

It works seamlessly with objects as well.

Setting Values Using Wildcards

You can update multiple items at once using *.

$items = [
    ['status' => 'pending'],
    ['status' => 'pending'],
];
data_set($items, '*.status', 'active');

Result:

[
    ['status' => 'active'],
    ['status' => 'active'],
]

Perfect for bulk updates.

Prevent Overwriting Existing Values

By default, data_set() overwrites values.

You can prevent this using the fourth parameter.

$config = ['debug' => true];
data_set($config, 'debug', false, false);

The original value remains unchanged.

Share: