Last active
July 18, 2016 20:25
-
-
Save lasergoat/2543d6b88c8821592ea759bc96f1e1c9 to your computer and use it in GitHub Desktop.
A simple solution for laravel's array helper called `array_only()` which support dot
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 | |
public function array_only($original, $keeping) | |
{ | |
// remove the following keys from the rendered bill object | |
$arr = []; | |
foreach ($keeping as $key) | |
{ | |
array_set($arr, $key, array_get($original, $key, null)); | |
} | |
return $arr; | |
}; | |
// use: | |
$original = $someModel->toArray(); | |
$keeping = [ | |
'id', | |
'status', | |
'viewed_at', | |
'paid_at', | |
'blah.company', | |
'customer.email', | |
'customer.firstname', | |
'customer.lastname', | |
'schedule.active', | |
'schedule.meta', | |
'schedule.payment_method.method', | |
'schedule.payment_method.card_last_four', | |
]; | |
array_only($original, $keeping); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment