Back to Homepage

data_forget Helper for Laravel

1 min read

Tags:

data_forget Helper for Laravel

Since Laravel version 10.15, there is a new utility function called data_forget that allows you to remove keys from an array or object using a "dot" notation.

Here is how the data_forget helper works:

$data = [
  'people' => [
    'john' => ['address' => '123 main', 'state' => 'nc'],
    'michael' => ['address' => '34 east 5th', 'state' => 'ny']
  ]
];
 
data_forget($data, 'people.*.address');

The data_forget function removes the 'address' attribute from both the "john" and "jane" sub-array. The result will then be:

[
    'people' => [
        'john' => ['state' => 'nc'],
        'michael' => ['state' => 'ny']
    ]
];

One of the neat features of the data_forget helper is its flexibility. It supports wildcard characters and complete 'dot' notation.

For instance, in the example above, the wildcard character (*) is used in the key string, specifying any sub-key under 'name'. Alternatively, if you need to remove a key from a specific sub-array, you can do so using the complete 'dot' notation, like so:

data_forget($data, 'people.john.address');

To learn more about the data_forget helper function, you can refer to the PR on Github.

Follow @LaravelSage on X → Follow @LaravelSage on Facebook →
Aniket Singh

Aniket Singh

View All Articles

Full-stack developer with a knack for Merging creativity with technical expertise for standout solutions.

Related Articles

Laravel Tenant Application with Tenancy

Laravel Tenant Application with Tenancy

You can make your Laravel app multi-tenant using the Tenancy for Laravel Tenant package. This tenancy package lets you make any Laravel application multi-tenant without rewriting it.

Top Laravel Packages for Building Powerful Applications

Top Laravel Packages for Building Powerful Applications

Are you ready to take your Laravel skills to the next level and build extraordinary applications? Look no further! In this blog post, we will unveil a treasure trove of top packages that will revolutionize your development process.

Subscribe for 20+ new Laravel tutorials every week

You can unsubscribe at any time. You'll also get -20% off my courses!

© 2024

 

Laravel Sage

   |    Privacy Policy