Skip to content

Instantly share code, notes, and snippets.

@yashbarot
Last active September 23, 2016 08:41
Show Gist options
  • Select an option

  • Save yashbarot/a82497a2d2cbd868b1113c9c02887c99 to your computer and use it in GitHub Desktop.

Select an option

Save yashbarot/a82497a2d2cbd868b1113c9c02887c99 to your computer and use it in GitHub Desktop.
List of Laravel Helper functions
<?php
/**
* Map function
*
* @return array
*/
function map($items, $func)
{
$result = [];
foreach ($items as $item) {
$result[] = $func($item);
}
return $result;
}
/**
* Basic sumOf function
*
* @param string column_name
* @param array $items
* @return double
*/
function sumOf($column_name, $items)
{
$result = 0;
foreach ($items as $item) {
$result += $item->$column_name;
}
return $result;
}
/**
* Get listOf values for specific key using collection
*
* @param string column_name
* @param array $items
* @return array
*/
function listsOf($column_name, $items)
{
$lists = [];
$collection = collect($items);
$lists = $collection->lists($column_name)->all();
return $lists;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment