Skip to content

Instantly share code, notes, and snippets.

@lasergoat
Last active July 18, 2016 20:25
Show Gist options
  • Save lasergoat/2543d6b88c8821592ea759bc96f1e1c9 to your computer and use it in GitHub Desktop.
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
<?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