Skip to content

Instantly share code, notes, and snippets.

@nowendwell
Last active July 25, 2023 15:23
Show Gist options
  • Select an option

  • Save nowendwell/929cb3b5ab98c0b4718cd4be4dc347f1 to your computer and use it in GitHub Desktop.

Select an option

Save nowendwell/929cb3b5ab98c0b4718cd4be4dc347f1 to your computer and use it in GitHub Desktop.

Revisions

  1. nowendwell revised this gist Jul 25, 2023. 1 changed file with 9 additions and 3 deletions.
    12 changes: 9 additions & 3 deletions PaginateCollectionProvider.php
    Original file line number Diff line number Diff line change
    @@ -28,13 +28,19 @@ public function register()
    // be sure to register this provider in config/app.php [providers]
    Collection::macro('paginate', function (int $perPage = 15, string $pageName = 'page', int $page = null, int $total = null, array $options = []): LengthAwarePaginator {
    $page = $page ?: LengthAwarePaginator::resolveCurrentPage($pageName);
    $results = $this->forPage($page, $perPage);
    $total = $total ?: $this->count();

    $options += [
    'path' => LengthAwarePaginator::resolveCurrentPath(),
    'pageName' => $pageName,
    ];
    return new LengthAwarePaginator($results, $total, $perPage, $page, $options);

    return new LengthAwarePaginator(
    $this->forPage($page, $perPage),
    $total ?: $this->count(),
    $perPage,
    $page,
    $options
    );
    });
    }
    }
  2. nowendwell revised this gist Mar 3, 2021. 1 changed file with 87 additions and 0 deletions.
    87 changes: 87 additions & 0 deletions Helpers.php
    Original file line number Diff line number Diff line change
    @@ -509,3 +509,90 @@ function number_suffix($number)
    }
    }
    }

    if (! function_exists('min_1')) {
    function min_1($number)
    {
    if (empty($number)) {
    return 1;
    }

    if ($number == '0.00') {
    return 1;
    }

    return $number;
    }
    }

    if (! function_exists('isJson')) {
    function isJson($string)
    {
    json_decode($string);

    return json_last_error() == JSON_ERROR_NONE;
    }
    }

    if (! function_exists('fileFaIcon')) {
    function fileFaIcon($extension)
    {
    switch (strtolower($extension)) {
    case 'pdf':
    return 'pdf';

    case 'document':
    case 'docx':
    case 'doc':
    case 'txt':
    return 'word';

    case 'csv':
    case 'xls':
    case 'xlsx':
    case 'numbers':
    case 'spreadsheet':
    return 'spreadsheet';

    case 'mp3':
    case 'ogg':
    case 'wav':
    return 'audio';

    case 'mp4':
    case 'mov':
    return 'video';

    case 'zip':
    case '7z':
    case 'rar':
    case 'tar':
    case 'dmg':
    return 'archive';

    case 'jpg':
    case 'jpeg':
    case 'png':
    case 'gif':
    case 'svg':
    case 'psd':
    return 'image';

    case 'ppt':
    case 'pptx':
    case 'presentation':
    return 'presentation';

    default:
    return 'alt';
    }
    }
    }

    if (! function_exists('byteFormat')) {
    function byteFormat($bytes) {
    if(empty($bytes)) { return ''; }
    $i = floor(log($bytes, 1000));
    return round($bytes / pow(1000, $i), [0,0,2,2,3][$i]). ' ' . ['B','KB','MB','GB','TB'][$i];
    }
    }
  3. nowendwell revised this gist Mar 3, 2021. 1 changed file with 259 additions and 2 deletions.
    261 changes: 259 additions & 2 deletions Helpers.php
    Original file line number Diff line number Diff line change
    @@ -144,13 +144,270 @@ function sortable_link($display, $value)
    }
    }

    if (! function_exists('get_countries')) {
    function get_countries()
    {
    return [
    'US' => 'United States',
    'CA' => 'Canada',
    'MX' => 'Mexico',
    'AF' => 'Afghanistan',
    'AX' => 'Åland Islands',
    'AL' => 'Albania',
    'DZ' => 'Algeria',
    'AS' => 'American Samoa',
    'AD' => 'Andorra',
    'AO' => 'Angola',
    'AI' => 'Anguilla',
    'AQ' => 'Antarctica',
    'AG' => 'Antigua & Barbuda',
    'AR' => 'Argentina',
    'AM' => 'Armenia',
    'AW' => 'Aruba',
    'AU' => 'Australia',
    'AT' => 'Austria',
    'AZ' => 'Azerbaijan',
    'BS' => 'Bahamas',
    'BH' => 'Bahrain',
    'BD' => 'Bangladesh',
    'BB' => 'Barbados',
    'BY' => 'Belarus',
    'BE' => 'Belgium',
    'BZ' => 'Belize',
    'BJ' => 'Benin',
    'BM' => 'Bermuda',
    'BT' => 'Bhutan',
    'BO' => 'Bolivia',
    'BA' => 'Bosnia & Herzegovina',
    'BW' => 'Botswana',
    'BV' => 'Bouvet Island',
    'BR' => 'Brazil',
    'IO' => 'British Indian Ocean Territory',
    'VG' => 'British Virgin Islands',
    'BN' => 'Brunei',
    'BG' => 'Bulgaria',
    'BF' => 'Burkina Faso',
    'BI' => 'Burundi',
    'KH' => 'Cambodia',
    'CM' => 'Cameroon',
    'CV' => 'Cape Verde',
    'BQ' => 'Caribbean Netherlands',
    'KY' => 'Cayman Islands',
    'CF' => 'Central African Republic',
    'TD' => 'Chad',
    'CL' => 'Chile',
    'CN' => 'China',
    'CX' => 'Christmas Island',
    'CC' => 'Cocos (Keeling) Islands',
    'CO' => 'Colombia',
    'KM' => 'Comoros',
    'CG' => 'Congo - Brazzaville',
    'CD' => 'Congo - Kinshasa',
    'CK' => 'Cook Islands',
    'CR' => 'Costa Rica',
    'CI' => 'Côte d’Ivoire',
    'HR' => 'Croatia',
    'CU' => 'Cuba',
    'CW' => 'Curaçao',
    'CY' => 'Cyprus',
    'CZ' => 'Czechia',
    'DK' => 'Denmark',
    'DJ' => 'Djibouti',
    'DM' => 'Dominica',
    'DO' => 'Dominican Republic',
    'EC' => 'Ecuador',
    'EG' => 'Egypt',
    'SV' => 'El Salvador',
    'GQ' => 'Equatorial Guinea',
    'ER' => 'Eritrea',
    'EE' => 'Estonia',
    'SZ' => 'Eswatini',
    'ET' => 'Ethiopia',
    'FK' => 'Falkland Islands',
    'FO' => 'Faroe Islands',
    'FJ' => 'Fiji',
    'FI' => 'Finland',
    'FR' => 'France',
    'GF' => 'French Guiana',
    'PF' => 'French Polynesia',
    'TF' => 'French Southern Territories',
    'GA' => 'Gabon',
    'GM' => 'Gambia',
    'GE' => 'Georgia',
    'DE' => 'Germany',
    'GH' => 'Ghana',
    'GI' => 'Gibraltar',
    'GR' => 'Greece',
    'GL' => 'Greenland',
    'GD' => 'Grenada',
    'GP' => 'Guadeloupe',
    'GU' => 'Guam',
    'GT' => 'Guatemala',
    'GG' => 'Guernsey',
    'GN' => 'Guinea',
    'GW' => 'Guinea-Bissau',
    'GY' => 'Guyana',
    'HT' => 'Haiti',
    'HM' => 'Heard & McDonald Islands',
    'HN' => 'Honduras',
    'HK' => 'Hong Kong SAR China',
    'HU' => 'Hungary',
    'IS' => 'Iceland',
    'IN' => 'India',
    'ID' => 'Indonesia',
    'IR' => 'Iran',
    'IQ' => 'Iraq',
    'IE' => 'Ireland',
    'IM' => 'Isle of Man',
    'IL' => 'Israel',
    'IT' => 'Italy',
    'JM' => 'Jamaica',
    'JP' => 'Japan',
    'JE' => 'Jersey',
    'JO' => 'Jordan',
    'KZ' => 'Kazakhstan',
    'KE' => 'Kenya',
    'KI' => 'Kiribati',
    'KW' => 'Kuwait',
    'KG' => 'Kyrgyzstan',
    'LA' => 'Laos',
    'LV' => 'Latvia',
    'LB' => 'Lebanon',
    'LS' => 'Lesotho',
    'LR' => 'Liberia',
    'LY' => 'Libya',
    'LI' => 'Liechtenstein',
    'LT' => 'Lithuania',
    'LU' => 'Luxembourg',
    'MO' => 'Macao SAR China',
    'MG' => 'Madagascar',
    'MW' => 'Malawi',
    'MY' => 'Malaysia',
    'MV' => 'Maldives',
    'ML' => 'Mali',
    'MT' => 'Malta',
    'MH' => 'Marshall Islands',
    'MQ' => 'Martinique',
    'MR' => 'Mauritania',
    'MU' => 'Mauritius',
    'YT' => 'Mayotte',
    'FM' => 'Micronesia',
    'MD' => 'Moldova',
    'MC' => 'Monaco',
    'MN' => 'Mongolia',
    'ME' => 'Montenegro',
    'MS' => 'Montserrat',
    'MA' => 'Morocco',
    'MZ' => 'Mozambique',
    'MM' => 'Myanmar (Burma)',
    'NA' => 'Namibia',
    'NR' => 'Nauru',
    'NP' => 'Nepal',
    'NL' => 'Netherlands',
    'NC' => 'New Caledonia',
    'NZ' => 'New Zealand',
    'NI' => 'Nicaragua',
    'NE' => 'Niger',
    'NG' => 'Nigeria',
    'NU' => 'Niue',
    'NF' => 'Norfolk Island',
    'KP' => 'North Korea',
    'MK' => 'North Macedonia',
    'MP' => 'Northern Mariana Islands',
    'NO' => 'Norway',
    'OM' => 'Oman',
    'PK' => 'Pakistan',
    'PW' => 'Palau',
    'PS' => 'Palestinian Territories',
    'PA' => 'Panama',
    'PG' => 'Papua New Guinea',
    'PY' => 'Paraguay',
    'PE' => 'Peru',
    'PH' => 'Philippines',
    'PN' => 'Pitcairn Islands',
    'PL' => 'Poland',
    'PT' => 'Portugal',
    'PR' => 'Puerto Rico',
    'QA' => 'Qatar',
    'RE' => 'Réunion',
    'RO' => 'Romania',
    'RU' => 'Russia',
    'RW' => 'Rwanda',
    'WS' => 'Samoa',
    'SM' => 'San Marino',
    'ST' => 'São Tomé & Príncipe',
    'SA' => 'Saudi Arabia',
    'SN' => 'Senegal',
    'RS' => 'Serbia',
    'SC' => 'Seychelles',
    'SL' => 'Sierra Leone',
    'SG' => 'Singapore',
    'SX' => 'Sint Maarten',
    'SK' => 'Slovakia',
    'SI' => 'Slovenia',
    'SB' => 'Solomon Islands',
    'SO' => 'Somalia',
    'ZA' => 'South Africa',
    'GS' => 'South Georgia & South Sandwich Islands',
    'KR' => 'South Korea',
    'SS' => 'South Sudan',
    'ES' => 'Spain',
    'LK' => 'Sri Lanka',
    'BL' => 'St. Barthélemy',
    'SH' => 'St. Helena',
    'KN' => 'St. Kitts & Nevis',
    'LC' => 'St. Lucia',
    'MF' => 'St. Martin',
    'PM' => 'St. Pierre & Miquelon',
    'VC' => 'St. Vincent & Grenadines',
    'SD' => 'Sudan',
    'SR' => 'Suriname',
    'SJ' => 'Svalbard & Jan Mayen',
    'SE' => 'Sweden',
    'CH' => 'Switzerland',
    'SY' => 'Syria',
    'TW' => 'Taiwan',
    'TJ' => 'Tajikistan',
    'TZ' => 'Tanzania',
    'TH' => 'Thailand',
    'TL' => 'Timor-Leste',
    'TG' => 'Togo',
    'TK' => 'Tokelau',
    'TO' => 'Tonga',
    'TT' => 'Trinidad & Tobago',
    'TN' => 'Tunisia',
    'TR' => 'Turkey',
    'TM' => 'Turkmenistan',
    'TC' => 'Turks & Caicos Islands',
    'TV' => 'Tuvalu',
    'UM' => 'U.S. Outlying Islands',
    'VI' => 'U.S. Virgin Islands',
    'UG' => 'Uganda',
    'UA' => 'Ukraine',
    'AE' => 'United Arab Emirates',
    'GB' => 'United Kingdom',
    'UY' => 'Uruguay',
    'UZ' => 'Uzbekistan',
    'VU' => 'Vanuatu',
    'VA' => 'Vatican City',
    'VE' => 'Venezuela',
    'VN' => 'Vietnam',
    'WF' => 'Wallis & Futuna',
    'EH' => 'Western Sahara',
    'YE' => 'Yemen',
    'ZM' => 'Zambia',
    'ZW' => 'Zimbabwe',
    ];
    }
    }

    /**
    * Simply a list of US states
    *
    * @return array Array of the US states
    */
    if (! function_exists('states')) {
    function states()
    if (! function_exists('get_states')) {
    function get_states()
    {
    return [
    "AK" => "Alaska",
  4. nowendwell revised this gist Aug 9, 2020. 2 changed files with 80 additions and 22 deletions.
    48 changes: 26 additions & 22 deletions Helpers.php
    Original file line number Diff line number Diff line change
    @@ -51,41 +51,45 @@ function currency($number, $decimal_places = 2)
    /**
    * Format date string as Carbon instance
    *
    * @param mixed $date Can be Carbon instance, DateTime instance, or date string
    * @param mixed $date Can be Carbon instance, DateTime instance, date string, or unix timestamp
    * @param string $format Date string to format into. If null, will return Carbon instance
    * @uses App\User::timezone to retrieve the user's timezone
    * @return mixed Will return formatted date string If format is not null, otherwise will return Carbon instance
    */
    if (! function_exists('carbon')) {
    function carbon($date, $format = null)
    function carbon($date, string $format = null)
    {
    // check to see if $date is Carbon
    $carbonized = null;

    // setup carbon instance
    if ($date instanceof \Carbon\Carbon) {
    if ($format != null) {
    return $date->format($format);
    } else {
    return $date;
    }
    $carbonized = $date;
    } else if ($date instanceof \DateTime) {
    $carbonized = Carbon::parse($date);
    } else if (@strtotime($date) && strtotime($date) != '1969-12-31') {
    $carbonized = Carbon::parse($date);
    } else if ( is_numeric($date) ) {
    $carbonized = Carbon::parse($date);
    } else {
    return null;
    }

    // check to see if $date is DateTime
    if ($date instanceof \DateTime) {
    if ($format != null) {
    return Carbon::parse($date)->format($format);
    } else {
    return Carbon::parse($date);
    // setup timezone
    $carbonized->settings(['timezone' => config('app.timezone', 'UTC')]);

    // update timezone
    if ( $carbonized !== null ) {
    if (auth()->check() && auth()->user()->timezone !== null) {
    $carbonized->setTimezone(auth()->user()->timezone);
    }
    }

    // if string and its a valid string return as Carbon
    if (strtotime($date) && strtotime($date) != '1969-12-31') {
    if ($format != null) {
    return Carbon::parse($date)->format($format);
    } else {
    return Carbon::parse($date);
    }
    //format
    if ($format != null) {
    return $carbonized->format($format);
    }

    return null;
    return $carbonized;
    }
    }

    54 changes: 54 additions & 0 deletions ViewServiceProvider.php
    Original file line number Diff line number Diff line change
    @@ -36,5 +36,59 @@ public function boot()
    Blade::directive('endPushOnce', function ($expression) {
    return '<?php $__env->stopPush(); endif; ?>';
    });

    Blade::directive('styles', function ($expression) {
    $style_path = $expression;

    // remove single quotes
    $style_path = str_replace("'", "", $style_path);

    // remove double quotes
    $style_path = str_replace('"', "", $style_path);

    if (Str::is('http*', $style_path)) {
    $asset = false;
    } else {
    $asset = true;
    }

    $key = '__pushonce_styles_' . str_replace(['-', '.'], '_', pathinfo($style_path, PATHINFO_FILENAME));

    $string = "<?php if(! isset(\$__env->{$key})): \$__env->{$key} = 1; \$__env->startPush('styles'); ?>";
    if ($asset) {
    $string .= '<link href="' . asset($style_path) . '" rel="stylesheet">';
    } else {
    $string .= '<link href="' . $style_path . '" rel="stylesheet">';
    }
    $string .= '<?php $__env->stopPush(); endif; ?>';
    return $string;
    });

    Blade::directive('scripts', function ($expression) {
    $script_path = $expression;

    // remove single quotes
    $script_path = str_replace("'", "", $script_path);

    // remove double quotes
    $script_path = str_replace('"', "", $script_path);

    if (Str::is('http*', $script_path)) {
    $asset = false;
    } else {
    $asset = true;
    }

    $key = '__pushonce_scripts_' . str_replace(['-','.'], '_', pathinfo($script_path, PATHINFO_FILENAME));

    $string = "<?php if(! isset(\$__env->{$key})): \$__env->{$key} = 1; \$__env->startPush('scripts'); ?>";
    if ($asset) {
    $string .= '<script src="' . asset($script_path) . '"></script>';
    } else {
    $string .= '<script src="' . $script_path . '"></script>';
    }
    $string .= '<?php $__env->stopPush(); endif; ?>';
    return $string;
    });
    }
    }
  5. nowendwell revised this gist Jul 1, 2020. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion ViewServiceProvider.php
    Original file line number Diff line number Diff line change
    @@ -5,7 +5,6 @@
    use Illuminate\Support\ServiceProvider;
    use Illuminate\Support\Facades\Blade;
    use Illuminate\Support\Facades\View;
    use App\Http\View\Composers\UserComposer;

    class ViewServiceProvider extends ServiceProvider
    {
  6. nowendwell revised this gist Jul 1, 2020. 1 changed file with 41 additions and 0 deletions.
    41 changes: 41 additions & 0 deletions ViewServiceProvider.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,41 @@
    <?php

    namespace App\Providers;

    use Illuminate\Support\ServiceProvider;
    use Illuminate\Support\Facades\Blade;
    use Illuminate\Support\Facades\View;
    use App\Http\View\Composers\UserComposer;

    class ViewServiceProvider extends ServiceProvider
    {
    /**
    * Register services.
    *
    * @return void
    */
    public function register()
    {
    //
    }

    /**
    * Bootstrap services.
    *
    * @return void
    */
    public function boot()
    {
    Blade::directive('pushOnce', function ($expression) {
    [$pushName, $pushSub] = explode(':', trim(substr($expression, 1, -1)));

    $key = '__pushonce_'.str_replace('-', '_', $pushName).'_'.str_replace('-', '_', $pushSub);

    return "<?php if(! isset(\$__env->{$key})): \$__env->{$key} = 1; \$__env->startPush('{$pushName}'); ?>";
    });

    Blade::directive('endPushOnce', function ($expression) {
    return '<?php $__env->stopPush(); endif; ?>';
    });
    }
    }
  7. nowendwell revised this gist Jan 8, 2020. 1 changed file with 8 additions and 0 deletions.
    8 changes: 8 additions & 0 deletions Helpers.php
    Original file line number Diff line number Diff line change
    @@ -17,6 +17,7 @@ function phone($data)
    }
    }
    }

    /**
    * Format a float to USD currency
    *
    @@ -46,6 +47,7 @@ function currency($number, $decimal_places = 2)
    return '$' . number_format($number, $decimal_places);
    }
    }

    /**
    * Format date string as Carbon instance
    *
    @@ -86,6 +88,7 @@ function carbon($date, $format = null)
    return null;
    }
    }

    /**
    * Simply echos selected on select fields
    *
    @@ -101,6 +104,7 @@ function selected($val1, $val2)
    }
    }
    }

    /**
    * Simply echos checked on input fields
    *
    @@ -116,6 +120,7 @@ function checked($val1, $val2)
    }
    }
    }

    /**
    * Adds column sorting to tables
    *
    @@ -134,6 +139,7 @@ function sortable_link($display, $value)
    echo '<a style="color: white" href="' . $url . '?sort=' . $value . '&sort_order=' . ($sort_order == 'desc' ? 'asc' : 'desc') . '&' . http_build_query(request()->except(['page','sort','sort_order'])) . '">' . $display . ' ' . $arrows . '</a>';
    }
    }

    /**
    * Simply a list of US states
    *
    @@ -201,6 +207,7 @@ function states()
    ];
    }
    }

    /**
    * Adds the git hash to the end of any asset tag
    * @param string $path The path to the asset you are trying to return
    @@ -215,6 +222,7 @@ function versioned_asset($path)
    }
    }
    }

    /**
    * Get suffix of a number
    *
  8. nowendwell revised this gist Jan 8, 2020. 1 changed file with 101 additions and 113 deletions.
    214 changes: 101 additions & 113 deletions Helpers.php
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,4 @@
    <?php

    use Illuminate\Support\Carbon;
    use Illuminate\Support\Facades\Storage;

    @@ -9,246 +8,235 @@
    * @param string $data 9161234567
    * @return string (916) 123-4567
    */
    if ( ! function_exists('phone') ) {
    if (! function_exists('phone')) {
    function phone($data)
    {
    if( preg_match( '/(\d{3})(\d{3})(\d{4})$/', $data, $matches ) ) {
    if (preg_match('/(\d{3})(\d{3})(\d{4})$/', $data, $matches)) {
    $result = "({$matches[1]})" . ' ' .$matches[2] . '-' . $matches[3];
    return $result;
    }
    }
    }

    /**
    * Format a float to USD currency
    *
    * @param float $number The number to format
    * @param int $decimal_places The number of decimal places to go to
    * @return string The currency representation as a string
    */
    if( !function_exists('currency')){
    function currency( $number, $decimal_places = 2 ) {

    if ( ! is_numeric($number) ) {
    if (!function_exists('currency')) {
    function currency($number, $decimal_places = 2)
    {
    if (! is_numeric($number)) {
    return $number;
    }

    // using NumberFormatter
    if (extension_loaded('intl')) {
    $formatter = new NumberFormatter('en_US', NumberFormatter::CURRENCY);
    if ( $decimal_places != 2 ) {
    if ($decimal_places != 2) {
    $formatter->setAttribute(NumberFormatter::FRACTION_DIGITS, $decimal_places);
    }
    return $formatter->formatCurrency($number, 'USD');
    }

    // if money_format available
    if ( version_compare(PHP_VERSION, '7.4.0') == -1 && function_exists('money_format') ) {
    if (version_compare(PHP_VERSION, '7.4.0') == -1 && function_exists('money_format')) {
    setlocale(LC_MONETARY, 'en_US');
    return money_format( '%.' . $decimal_places . 'n', $number );
    return money_format('%.' . $decimal_places . 'n', $number);
    }

    return '$' . number_format($number, $decimal_places);
    }
    }

    /**
    * Format date string as Carbon instance
    *
    * @param mixed $date Can be Carbon instance, DateTime instance, or date string
    * @param string $format Date string to format into. If null, will return Carbon instance
    * @return mixed Will return formatted date string If format is not null, otherwise will return Carbon instance
    */
    if ( ! function_exists('carbon') ) {
    function carbon( $date, $format = null )
    if (! function_exists('carbon')) {
    function carbon($date, $format = null)
    {
    // check to see if $date is Carbon
    if ($date instanceof \Carbon\Carbon) {
    if ( $format != null ) {
    return $date->format( $format );
    if ($format != null) {
    return $date->format($format);
    } else {
    return $date;
    }
    }

    // check to see if $date is DateTime
    if ($date instanceof \DateTime) {
    if ( $format != null ) {
    return Carbon::parse( $date )->format( $format );
    if ($format != null) {
    return Carbon::parse($date)->format($format);
    } else {
    return Carbon::parse( $date );
    return Carbon::parse($date);
    }
    }

    // if string and its a valid string return as Carbon
    if ( strtotime( $date ) && strtotime( $date ) != '1969-12-31') {
    if ( $format != null ) {
    return Carbon::parse( $date )->format( $format );
    if (strtotime($date) && strtotime($date) != '1969-12-31') {
    if ($format != null) {
    return Carbon::parse($date)->format($format);
    } else {
    return Carbon::parse( $date );
    return Carbon::parse($date);
    }
    }

    return null;
    }
    }

    /**
    * Simply echos selected on select fields
    *
    * @param string|integer $val1 The first value to compare
    * @param string|integer $val2 The second value to compare
    * @return void Will echo selected if equal, otherwise nothing
    */
    if ( ! function_exists('selected') ) {
    function selected( $val1, $val2 )
    if (! function_exists('selected')) {
    function selected($val1, $val2)
    {
    if ( $val1 == $val2 )
    {
    if ($val1 == $val2) {
    echo 'selected="selected"';
    }
    }
    }

    /**
    * Simply echos checked on input fields
    *
    * @param string|integer $val1 The first value to compare
    * @param string|integer $val2 The second value to compare
    * @return void Will echo selected if equal, otherwise nothing
    */
    if ( ! function_exists('checked') ) {
    function checked( $val1, $val2 )
    if (! function_exists('checked')) {
    function checked($val1, $val2)
    {
    if ( $val1 == $val2 )
    {
    if ($val1 == $val2) {
    echo 'checked="checked"';
    }
    }
    }

    /**
    * Adds column sorting to tables
    *
    * @param string $display The value you want to show the user
    * @param string $value The value you want to pass to the server
    * @return void Echos string
    */
    if ( ! function_exists('sortable_link') ) {
    if (! function_exists('sortable_link')) {
    function sortable_link($display, $value)
    {
    $url = url()->current();
    $sort = request()->sort;
    $sort_order = request()->sort_order;
    $arrows = $sort == $value ? strtolower( request()->sort_order ) == 'desc' ? '<i class="fa fa-caret-up" style="vertical-align:top"></i>' : '<i class="fa fa-caret-down" style="vertical-align:top"></i>' : '';
    echo '<a style="color: white" href="' . $url . '?sort=' . $value . '&sort_order=' . ( $sort_order == 'desc' ? 'asc' : 'desc' ) . '&' . http_build_query ( request()->except(['page','sort','sort_order']) ) . '">' . $display . ' ' . $arrows . '</a>';
    $arrows = $sort == $value ? strtolower(request()->sort_order) == 'desc' ? '<i class="fa fa-caret-up" style="vertical-align:top"></i>' : '<i class="fa fa-caret-down" style="vertical-align:top"></i>' : '';

    echo '<a style="color: white" href="' . $url . '?sort=' . $value . '&sort_order=' . ($sort_order == 'desc' ? 'asc' : 'desc') . '&' . http_build_query(request()->except(['page','sort','sort_order'])) . '">' . $display . ' ' . $arrows . '</a>';
    }
    }

    /**
    * Simply a list of US states
    *
    * @return array Array of the US states
    */
    if ( ! function_exists('states') ) {
    if (! function_exists('states')) {
    function states()
    {
    return [
    "AK" => "Alaska",
    "AL" => "Alabama",
    "AR" => "Arkansas",
    "AS" => "American Samoa",
    "AZ" => "Arizona",
    "CA" => "California",
    "CO" => "Colorado",
    "CT" => "Connecticut",
    "DC" => "District of Columbia",
    "DE" => "Delaware",
    "FL" => "Florida",
    "GA" => "Georgia",
    "GU" => "Guam",
    "HI" => "Hawaii",
    "IA" => "Iowa",
    "ID" => "Idaho",
    "IL" => "Illinois",
    "IN" => "Indiana",
    "KS" => "Kansas",
    "KY" => "Kentucky",
    "LA" => "Louisiana",
    "MA" => "Massachusetts",
    "MD" => "Maryland",
    "ME" => "Maine",
    "MI" => "Michigan",
    "MN" => "Minnesota",
    "MO" => "Missouri",
    "MS" => "Mississippi",
    "MT" => "Montana",
    "NC" => "North Carolina",
    "ND" => "North Dakota",
    "NE" => "Nebraska",
    "NH" => "New Hampshire",
    "NJ" => "New Jersey",
    "NM" => "New Mexico",
    "NV" => "Nevada",
    "NY" => "New York",
    "OH" => "Ohio",
    "OK" => "Oklahoma",
    "OR" => "Oregon",
    "PA" => "Pennsylvania",
    "PR" => "Puerto Rico",
    "RI" => "Rhode Island",
    "SC" => "South Carolina",
    "SD" => "South Dakota",
    "TN" => "Tennessee",
    "TX" => "Texas",
    "UT" => "Utah",
    "VA" => "Virginia",
    "VI" => "Virgin Islands",
    "VT" => "Vermont",
    "WA" => "Washington",
    "WI" => "Wisconsin",
    "WV" => "West Virginia",
    "AK" => "Alaska",
    "AL" => "Alabama",
    "AR" => "Arkansas",
    "AS" => "American Samoa",
    "AZ" => "Arizona",
    "CA" => "California",
    "CO" => "Colorado",
    "CT" => "Connecticut",
    "DC" => "District of Columbia",
    "DE" => "Delaware",
    "FL" => "Florida",
    "GA" => "Georgia",
    "GU" => "Guam",
    "HI" => "Hawaii",
    "IA" => "Iowa",
    "ID" => "Idaho",
    "IL" => "Illinois",
    "IN" => "Indiana",
    "KS" => "Kansas",
    "KY" => "Kentucky",
    "LA" => "Louisiana",
    "MA" => "Massachusetts",
    "MD" => "Maryland",
    "ME" => "Maine",
    "MI" => "Michigan",
    "MN" => "Minnesota",
    "MO" => "Missouri",
    "MS" => "Mississippi",
    "MT" => "Montana",
    "NC" => "North Carolina",
    "ND" => "North Dakota",
    "NE" => "Nebraska",
    "NH" => "New Hampshire",
    "NJ" => "New Jersey",
    "NM" => "New Mexico",
    "NV" => "Nevada",
    "NY" => "New York",
    "OH" => "Ohio",
    "OK" => "Oklahoma",
    "OR" => "Oregon",
    "PA" => "Pennsylvania",
    "PR" => "Puerto Rico",
    "RI" => "Rhode Island",
    "SC" => "South Carolina",
    "SD" => "South Dakota",
    "TN" => "Tennessee",
    "TX" => "Texas",
    "UT" => "Utah",
    "VA" => "Virginia",
    "VI" => "Virgin Islands",
    "VT" => "Vermont",
    "WA" => "Washington",
    "WI" => "Wisconsin",
    "WV" => "West Virginia",
    "WY" => "Wyoming"
    ];
    }
    }

    /**
    * Adds the git hash to the end of any asset tag
    * @param string $path The path to the asset you are trying to return
    */
    if ( ! function_exists('versioned_asset') ) {
    if (! function_exists('versioned_asset')) {
    function versioned_asset($path)
    {
    if ( Storage::exists('hash.txt') ) {
    if (Storage::exists('hash.txt')) {
    return asset($path) . '?v=' . Storage::get('hash.txt');
    } else {
    return asset($path);
    }
    }
    }


    /**
    * Get suffix of a number
    *
    * @param int $number 50
    * @return string The number and suffix
    */
    if ( ! function_exists('number_suffix') ) {
    function number_suffix($number) {
    if ( $number == null ) return;
    if ( ! is_numeric($number) ) return $number;

    if (! function_exists('number_suffix')) {
    function number_suffix($number)
    {
    if ($number == null) {
    return;
    }
    if (! is_numeric($number)) {
    return $number;
    }
    $ends = array('th','st','nd','rd','th','th','th','th','th','th');
    $mod = $number % 100;

    // 11th, 12th, 13th
    if ( $mod >= 11 && $mod <= 13 ) {
    if ($mod >= 11 && $mod <= 13) {
    return $number . 'th';
    } else {
    return $number . $ends[$number % 10];
    }
    }
    }
    }
  9. nowendwell revised this gist Jan 8, 2020. 1 changed file with 27 additions and 10 deletions.
    37 changes: 27 additions & 10 deletions Helpers.php
    Original file line number Diff line number Diff line change
    @@ -20,18 +20,35 @@ function phone($data)
    }

    /**
    * Format decimal as US currency
    * Format a float to USD currency
    *
    * @param float $number 100.50
    * @param integer $decimal_places Number of decimal places to display (default: 2)
    * @return mixed Integer or Float depending on the $decimal_places param
    * @param float $number The number to format
    * @param int $decimal_places The number of decimal places to go to
    * @return string The currency representation as a string
    */
    if ( ! function_exists('currency') ) {
    function currency( $number, $decimal_places = 2 )
    {
    if ( ! is_numeric($number) ) { return $number; }
    setlocale(LC_MONETARY, 'en_US');
    return money_format( '%.' . $decimal_places . 'n', $number );
    if( !function_exists('currency')){
    function currency( $number, $decimal_places = 2 ) {

    if ( ! is_numeric($number) ) {
    return $number;
    }

    // using NumberFormatter
    if (extension_loaded('intl')) {
    $formatter = new NumberFormatter('en_US', NumberFormatter::CURRENCY);
    if ( $decimal_places != 2 ) {
    $formatter->setAttribute(NumberFormatter::FRACTION_DIGITS, $decimal_places);
    }
    return $formatter->formatCurrency($number, 'USD');
    }

    // if money_format available
    if ( version_compare(PHP_VERSION, '7.4.0') == -1 && function_exists('money_format') ) {
    setlocale(LC_MONETARY, 'en_US');
    return money_format( '%.' . $decimal_places . 'n', $number );
    }

    return '$' . number_format($number, $decimal_places);
    }
    }

  10. nowendwell renamed this gist Oct 28, 2019. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  11. nowendwell revised this gist Oct 28, 2019. No changes.
  12. nowendwell revised this gist Jun 29, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion helpers.php
    Original file line number Diff line number Diff line change
    @@ -123,7 +123,7 @@ function sortable_link($display, $value)
    $url = url()->current();
    $sort = request()->sort;
    $sort_order = request()->sort_order;
    $arrows = request()->sort == $value ? strtolower( request()->sort_order ) == 'desc' ? '<i class="fa fa-caret-up" style="vertical-align:top"></i>' : '<i class="fa fa-caret-down" style="vertical-align:top"></i>' : '';
    $arrows = $sort == $value ? strtolower( request()->sort_order ) == 'desc' ? '<i class="fa fa-caret-up" style="vertical-align:top"></i>' : '<i class="fa fa-caret-down" style="vertical-align:top"></i>' : '';

    echo '<a style="color: white" href="' . $url . '?sort=' . $value . '&sort_order=' . ( $sort_order == 'desc' ? 'asc' : 'desc' ) . '&' . http_build_query ( request()->except(['page','sort','sort_order']) ) . '">' . $display . ' ' . $arrows . '</a>';
    }
  13. nowendwell revised this gist Mar 22, 2019. 1 changed file with 24 additions and 0 deletions.
    24 changes: 24 additions & 0 deletions helpers.php
    Original file line number Diff line number Diff line change
    @@ -210,4 +210,28 @@ function versioned_asset($path)
    return asset($path);
    }
    }
    }


    /**
    * Get suffix of a number
    *
    * @param int $number 50
    * @return string The number and suffix
    */
    if ( ! function_exists('number_suffix') ) {
    function number_suffix($number) {
    if ( $number == null ) return;
    if ( ! is_numeric($number) ) return $number;

    $ends = array('th','st','nd','rd','th','th','th','th','th','th');
    $mod = $number % 100;

    // 11th, 12th, 13th
    if ( $mod >= 11 && $mod <= 13 ) {
    return $number . 'th';
    } else {
    return $number . $ends[$number % 10];
    }
    }
    }
  14. nowendwell revised this gist Feb 28, 2019. 1 changed file with 40 additions and 0 deletions.
    40 changes: 40 additions & 0 deletions PaginateCollectionProvider.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,40 @@
    <?php

    namespace App\Providers;

    use Illuminate\Support\ServiceProvider;
    use Illuminate\Support\Collection;
    use Illuminate\Pagination\LengthAwarePaginator;

    class PaginateCollectionProvider extends ServiceProvider
    {
    /**
    * Bootstrap services.
    *
    * @return void
    */
    public function boot()
    {
    //
    }

    /**
    * Register services.
    *
    * @return void
    */
    public function register()
    {
    // be sure to register this provider in config/app.php [providers]
    Collection::macro('paginate', function (int $perPage = 15, string $pageName = 'page', int $page = null, int $total = null, array $options = []): LengthAwarePaginator {
    $page = $page ?: LengthAwarePaginator::resolveCurrentPage($pageName);
    $results = $this->forPage($page, $perPage);
    $total = $total ?: $this->count();
    $options += [
    'path' => LengthAwarePaginator::resolveCurrentPath(),
    'pageName' => $pageName,
    ];
    return new LengthAwarePaginator($results, $total, $perPage, $page, $options);
    });
    }
    }
  15. nowendwell revised this gist Jan 8, 2019. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions helpers.php
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    <?php

    use \Carbon\Carbon;
    use \Storage;
    use Illuminate\Support\Carbon;
    use Illuminate\Support\Facades\Storage;

    /**
    * Format phone number to standard US
  16. nowendwell revised this gist Oct 5, 2018. 1 changed file with 16 additions and 0 deletions.
    16 changes: 16 additions & 0 deletions helpers.php
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,7 @@
    <?php

    use \Carbon\Carbon;
    use \Storage;

    /**
    * Format phone number to standard US
    @@ -194,4 +195,19 @@ function states()
    "WY" => "Wyoming"
    ];
    }
    }

    /**
    * Adds the git hash to the end of any asset tag
    * @param string $path The path to the asset you are trying to return
    */
    if ( ! function_exists('versioned_asset') ) {
    function versioned_asset($path)
    {
    if ( Storage::exists('hash.txt') ) {
    return asset($path) . '?v=' . Storage::get('hash.txt');
    } else {
    return asset($path);
    }
    }
    }
  17. nowendwell revised this gist Sep 21, 2018. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions helpers.php
    Original file line number Diff line number Diff line change
    @@ -28,6 +28,7 @@ function phone($data)
    if ( ! function_exists('currency') ) {
    function currency( $number, $decimal_places = 2 )
    {
    if ( ! is_numeric($number) ) { return $number; }
    setlocale(LC_MONETARY, 'en_US');
    return money_format( '%.' . $decimal_places . 'n', $number );
    }
  18. nowendwell revised this gist Apr 17, 2018. 1 changed file with 11 additions and 11 deletions.
    22 changes: 11 additions & 11 deletions helpers.php
    Original file line number Diff line number Diff line change
    @@ -11,9 +11,9 @@
    if ( ! function_exists('phone') ) {
    function phone($data)
    {
    if( preg_match( '/(\d{3})(\d{3})(\d{4})$/', $data, $matches ) ) {
    $result = "({$matches[1]})" . ' ' .$matches[2] . '-' . $matches[3];
    return $result;
    if( preg_match( '/(\d{3})(\d{3})(\d{4})$/', $data, $matches ) ) {
    $result = "({$matches[1]})" . ' ' .$matches[2] . '-' . $matches[3];
    return $result;
    }
    }
    }
    @@ -29,7 +29,7 @@ function phone($data)
    function currency( $number, $decimal_places = 2 )
    {
    setlocale(LC_MONETARY, 'en_US');
    return money_format( '%.' . $decimal_places . 'n', $number );
    return money_format( '%.' . $decimal_places . 'n', $number );
    }
    }

    @@ -51,7 +51,7 @@ function carbon( $date, $format = null )
    return $date;
    }
    }

    // check to see if $date is DateTime
    if ($date instanceof \DateTime) {
    if ( $format != null ) {
    @@ -60,17 +60,17 @@ function carbon( $date, $format = null )
    return Carbon::parse( $date );
    }
    }

    // if string and its a valid string return as Carbon
    if ( strtotime( $date ) && strtotime( $date ) != '1969-12-31') {
    if ( strtotime( $date ) && strtotime( $date ) != '1969-12-31') {
    if ( $format != null ) {
    return Carbon::parse( $date )->format( $format );
    } else {
    return Carbon::parse( $date );
    }
    }

    return null;
    }
    return null;
    }
    }

    @@ -122,7 +122,7 @@ function sortable_link($display, $value)
    $sort = request()->sort;
    $sort_order = request()->sort_order;
    $arrows = request()->sort == $value ? strtolower( request()->sort_order ) == 'desc' ? '<i class="fa fa-caret-up" style="vertical-align:top"></i>' : '<i class="fa fa-caret-down" style="vertical-align:top"></i>' : '';

    echo '<a style="color: white" href="' . $url . '?sort=' . $value . '&sort_order=' . ( $sort_order == 'desc' ? 'asc' : 'desc' ) . '&' . http_build_query ( request()->except(['page','sort','sort_order']) ) . '">' . $display . ' ' . $arrows . '</a>';
    }
    }
  19. nowendwell revised this gist Apr 17, 2018. 1 changed file with 0 additions and 2 deletions.
    2 changes: 0 additions & 2 deletions helpers.php
    Original file line number Diff line number Diff line change
    @@ -2,8 +2,6 @@

    use \Carbon\Carbon;



    /**
    * Format phone number to standard US
    *
  20. nowendwell created this gist Apr 17, 2018.
    198 changes: 198 additions & 0 deletions helpers.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,198 @@
    <?php

    use \Carbon\Carbon;



    /**
    * Format phone number to standard US
    *
    * @param string $data 9161234567
    * @return string (916) 123-4567
    */
    if ( ! function_exists('phone') ) {
    function phone($data)
    {
    if( preg_match( '/(\d{3})(\d{3})(\d{4})$/', $data, $matches ) ) {
    $result = "({$matches[1]})" . ' ' .$matches[2] . '-' . $matches[3];
    return $result;
    }
    }
    }

    /**
    * Format decimal as US currency
    *
    * @param float $number 100.50
    * @param integer $decimal_places Number of decimal places to display (default: 2)
    * @return mixed Integer or Float depending on the $decimal_places param
    */
    if ( ! function_exists('currency') ) {
    function currency( $number, $decimal_places = 2 )
    {
    setlocale(LC_MONETARY, 'en_US');
    return money_format( '%.' . $decimal_places . 'n', $number );
    }
    }

    /**
    * Format date string as Carbon instance
    *
    * @param mixed $date Can be Carbon instance, DateTime instance, or date string
    * @param string $format Date string to format into. If null, will return Carbon instance
    * @return mixed Will return formatted date string If format is not null, otherwise will return Carbon instance
    */
    if ( ! function_exists('carbon') ) {
    function carbon( $date, $format = null )
    {
    // check to see if $date is Carbon
    if ($date instanceof \Carbon\Carbon) {
    if ( $format != null ) {
    return $date->format( $format );
    } else {
    return $date;
    }
    }

    // check to see if $date is DateTime
    if ($date instanceof \DateTime) {
    if ( $format != null ) {
    return Carbon::parse( $date )->format( $format );
    } else {
    return Carbon::parse( $date );
    }
    }

    // if string and its a valid string return as Carbon
    if ( strtotime( $date ) && strtotime( $date ) != '1969-12-31') {
    if ( $format != null ) {
    return Carbon::parse( $date )->format( $format );
    } else {
    return Carbon::parse( $date );
    }
    }

    return null;
    }
    }

    /**
    * Simply echos selected on select fields
    *
    * @param string|integer $val1 The first value to compare
    * @param string|integer $val2 The second value to compare
    * @return void Will echo selected if equal, otherwise nothing
    */
    if ( ! function_exists('selected') ) {
    function selected( $val1, $val2 )
    {
    if ( $val1 == $val2 )
    {
    echo 'selected="selected"';
    }
    }
    }

    /**
    * Simply echos checked on input fields
    *
    * @param string|integer $val1 The first value to compare
    * @param string|integer $val2 The second value to compare
    * @return void Will echo selected if equal, otherwise nothing
    */
    if ( ! function_exists('checked') ) {
    function checked( $val1, $val2 )
    {
    if ( $val1 == $val2 )
    {
    echo 'checked="checked"';
    }
    }
    }

    /**
    * Adds column sorting to tables
    *
    * @param string $display The value you want to show the user
    * @param string $value The value you want to pass to the server
    * @return void Echos string
    */
    if ( ! function_exists('sortable_link') ) {
    function sortable_link($display, $value)
    {
    $url = url()->current();
    $sort = request()->sort;
    $sort_order = request()->sort_order;
    $arrows = request()->sort == $value ? strtolower( request()->sort_order ) == 'desc' ? '<i class="fa fa-caret-up" style="vertical-align:top"></i>' : '<i class="fa fa-caret-down" style="vertical-align:top"></i>' : '';

    echo '<a style="color: white" href="' . $url . '?sort=' . $value . '&sort_order=' . ( $sort_order == 'desc' ? 'asc' : 'desc' ) . '&' . http_build_query ( request()->except(['page','sort','sort_order']) ) . '">' . $display . ' ' . $arrows . '</a>';
    }
    }

    /**
    * Simply a list of US states
    *
    * @return array Array of the US states
    */
    if ( ! function_exists('states') ) {
    function states()
    {
    return [
    "AK" => "Alaska",
    "AL" => "Alabama",
    "AR" => "Arkansas",
    "AS" => "American Samoa",
    "AZ" => "Arizona",
    "CA" => "California",
    "CO" => "Colorado",
    "CT" => "Connecticut",
    "DC" => "District of Columbia",
    "DE" => "Delaware",
    "FL" => "Florida",
    "GA" => "Georgia",
    "GU" => "Guam",
    "HI" => "Hawaii",
    "IA" => "Iowa",
    "ID" => "Idaho",
    "IL" => "Illinois",
    "IN" => "Indiana",
    "KS" => "Kansas",
    "KY" => "Kentucky",
    "LA" => "Louisiana",
    "MA" => "Massachusetts",
    "MD" => "Maryland",
    "ME" => "Maine",
    "MI" => "Michigan",
    "MN" => "Minnesota",
    "MO" => "Missouri",
    "MS" => "Mississippi",
    "MT" => "Montana",
    "NC" => "North Carolina",
    "ND" => "North Dakota",
    "NE" => "Nebraska",
    "NH" => "New Hampshire",
    "NJ" => "New Jersey",
    "NM" => "New Mexico",
    "NV" => "Nevada",
    "NY" => "New York",
    "OH" => "Ohio",
    "OK" => "Oklahoma",
    "OR" => "Oregon",
    "PA" => "Pennsylvania",
    "PR" => "Puerto Rico",
    "RI" => "Rhode Island",
    "SC" => "South Carolina",
    "SD" => "South Dakota",
    "TN" => "Tennessee",
    "TX" => "Texas",
    "UT" => "Utah",
    "VA" => "Virginia",
    "VI" => "Virgin Islands",
    "VT" => "Vermont",
    "WA" => "Washington",
    "WI" => "Wisconsin",
    "WV" => "West Virginia",
    "WY" => "Wyoming"
    ];
    }
    }