Created
September 6, 2021 22:56
-
-
Save uchm4n/c97d65f7ba97b8b563bbd2fb97cbaf52 to your computer and use it in GitHub Desktop.
Get Collection data like you would with data_get() helper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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