Skip to content

Instantly share code, notes, and snippets.

@uchm4n
Created September 6, 2021 22:56
Show Gist options
  • Save uchm4n/c97d65f7ba97b8b563bbd2fb97cbaf52 to your computer and use it in GitHub Desktop.
Save uchm4n/c97d65f7ba97b8b563bbd2fb97cbaf52 to your computer and use it in GitHub Desktop.
Get Collection data like you would with data_get() helper
<?php
// Register collection macros in the boot method of a app service provider
// app/Providers/AppServiceProvider.php
// usage: collect([...])->get()->data_get('key.someData.0.email','Default')
Collection::macro('data_get', function ($key, $default = '') {
return $this->map(function ($value) use ($key, $default) {
return data_get($value, $key, $default);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment