2017 © Pedro Peláez
 

library laravel-helpers

An extensive set of Laravel framework helper functions and collection macros.

image

sebastiaanluca/laravel-helpers

An extensive set of Laravel framework helper functions and collection macros.

  • Monday, July 23, 2018
  • by sebastiaanluca
  • Repository
  • 12 Watchers
  • 378 Stars
  • 11,965 Installations
  • PHP
  • 5 Dependents
  • 0 Suggesters
  • 19 Forks
  • 0 Open issues
  • 17 Versions
  • 12 % Grown

The README.md

, (*1)

Latest stable release Software license Build status Total downloads Total downloads , (*2)

Read my blog View my other packages and projects Follow @sebastiaanluca on Twitter Share this package on Twitter , (*3)

An extensive set of Laravel framework helper functions and collection macros. , (*4)

Table of contents

Requirements

  • PHP 7.3 or higher
  • Laravel 7.0 or higher

How to install

Just add the package to your project using Composer and Laravel will auto-discover it:, (*5)

composer require sebastiaanluca/laravel-helpers

If you want to use the collection debug macros, install the kint-php/kint package as a dev dependency:, (*6)

composer require kint-php/kint --dev

Upgrading from 1.x

All essential generic PHP helpers have been extracted to their own sebastiaanluca/php-helpers package and some other helpers have been removed in anticipation of their own package. In effect and from now on, Laravel Helpers will only contain helpers for the Laravel framework., (*7)

See the changelog for more information., (*8)

Framework helper functions

locale

Get the active app locale or the fallback locale if it's missing or not set., (*9)

locale();

// "en"

is_guest

Determine if the current user is a guest., (*10)

The opposite of is_logged_in., (*11)

// When not authenticated
is_guest();

// true

// When authenticated as a user
is_guest();

// false

is_logged_in

Determine if the current user is authenticated., (*12)

The opposite of is_guest., (*13)

// When not authenticated
is_logged_in();

// false

// When authenticated as a user
is_logged_in();

// true

user

Get the currently authenticated user (if there is one)., (*14)

When logged in, returns your user model or object that implements \Illuminate\Contracts\Auth\Authenticatable., (*15)

// When not authenticated
user();

// null

// When authenticated as a user
user();

// Illuminate\Foundation\Auth\User {}

me

Get the currently authenticated user (if there is one)., (*16)

When logged in, returns your user model or object that implements \Illuminate\Contracts\Auth\Authenticatable., (*17)

An alternative for user., (*18)

// When not authenticated
me();

// null

// When authenticated as a user
me();

// Illuminate\Foundation\Auth\User {}

Collection macros

carbonize

Create Carbon instances from items in a collection., (*19)

collect([
    'yesterday',
    'tomorrow',
    '2017-07-01',
])->carbonize();

/*
Illuminate\Support\Collection {
    all: [
        Carbon\Carbon {
            "date": "2017-07-09 00:00:00.000000",
            "timezone_type": 3,
            "timezone": "UTC",
        },
        Carbon\Carbon {
            "date": "2017-07-11 00:00:00.000000",
            "timezone_type": 3,
            "timezone": "UTC",
        },
        Carbon\Carbon {
            "date": "2017-07-01 00:00:00.000000",
            "timezone_type": 3,
            "timezone": "UTC",
        },
    ],
}
*/

between

Reduce each collection item to the value found between a given start and end string., (*20)

The second parameter is optional and falls back to the start string if null., (*21)

collect([
    '"value1"',
    '"value2"',
    '"value3"',
])->between('"', '"');

/*
Illuminate\Support\Collection {
    all: [
        "value1",
        "value2",
        "value3",
    ],
}
*/

transformKeys

Perform an operation on the collection's keys., (*22)

The callable operation can either be a globally available method or a closure., (*23)

collect([
    'a' => 'value',
    'b' => 'value',
    'c' => 'value',
])->transformKeys('strtoupper');

/*
Illuminate\Support\Collection {
    all: [
        "A" => "value",
        "B" => "value",
        "C" => "value",
    ],
}
*/
collect([
    'a' => 'value',
    'b' => 'value',
    'c' => 'value',
])->transformKeys(function (string $key) {
    return 'prefix-' . $key;
});

/*
Illuminate\Support\Collection {
    all: [
        "prefix-a" => "value",
        "prefix-b" => "value",
        "prefix-c" => "value",
    ],
}
*/

transpose

Transpose (flip) a collection matrix (array of arrays) so its columns become rows and its rows become columns., (*24)

collect([
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9],
])->transpose();

/*
Illuminate\Support\Collection {
    all: [
        [1, 4, 7],
        [2, 5, 8],
        [3, 6, 9],
    ],
}
*/

transposeWithKeys

Flip a collection of rows and values per column so its columns become rows and its rows become columns., (*25)

Before:, (*26)

id name
A 1 James
B 2 Joe
C 3 Jonas

After:, (*27)

A B C
id 1 2 3
name James Joe Jonas

How to use:, (*28)

collect([
    'A' => [
        'id' => 1,
        'name' => 'James',
    ],
    'B' => [
        'id' => 2,
        'name' => 'Joe',
    ],
    'C' => [
        'id' => 3,
        'name' => 'Jonas',
    ],
])->transposeWithKeys();

/*
Illuminate\Support\Collection {
    all: [
        "id" => [
            "A" => 1,
            "B" => 2,
            "C" => 3,
        ],
        "name" => [
            "A" => "James",
            "B" => "Joe",
            "C" => "Jonas",
        ],
    ],
}
*/

You can also pass some row header names if you don't want them to be automatically guessed. You'd then call the macro with transposeWithKeys(['myID', 'row2']) and the resulting rows would be myID and row2 instead of id and name respectively., (*29)

d

Display structured debug information on the collection using Kint. Can be called multiple times during a collection's method chain and outputs debug information at each point of use. Continues script execution afterwards., (*30)

Explicitly requires the kint-php/kint package., (*31)

collect([
    'id' => 6,
    'name' => 'Sebastiaan',
])
->d()
->put('role', 'author')
->d();

ddd

Display structured debug information on the collection using Kint. Halts script execution afterwards, so it can only be called once during a collection's method chain., (*32)

Explicitly requires the kint-php/kint package., (*33)

collect([
    'id' => 6,
    'name' => 'Sebastiaan',
])
->d()
->put('role', 'author')
->ddd();

License

This package operates under the MIT License (MIT). Please see LICENSE for more information., (*34)

Change log

Please see CHANGELOG for more information what has changed recently., (*35)

Testing

composer install
composer test

Contributing

Please see CONTRIBUTING and CODE OF CONDUCT for details., (*36)

Security

If you discover any security related issues, please email hello@sebastiaanluca.com instead of using the issue tracker., (*37)

Credits

About

My name is Sebastiaan and I'm a freelance Laravel developer specializing in building custom Laravel applications. Check out my portfolio for more information, my blog for the latest tips and tricks, and my other packages to kick-start your next project., (*38)

Have a project that could use some guidance? Send me an e-mail at hello@sebastiaanluca.com!, (*39)

The Versions

23/07 2018

dev-develop

dev-develop https://github.com/sebastiaanluca/laravel-helpers

An extensive set of Laravel framework helper functions and collection macros.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel helpers collection form helper array blade response collections pipe macros

22/07 2018

dev-master

9999999-dev https://github.com/sebastiaanluca/laravel-helpers

An extensive set of Laravel framework helper functions and collection macros.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel helpers collection form helper array blade response collections pipe macros

22/07 2018

2.0.0

2.0.0.0 https://github.com/sebastiaanluca/laravel-helpers

An extensive set of Laravel framework helper functions and collection macros.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel helpers collections macros

22/07 2018

dev-feature/extract-helpers

dev-feature/extract-helpers https://github.com/sebastiaanluca/laravel-helpers

An extensive set of Laravel framework helper functions and collection macros.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel helpers collections macros

25/04 2017

0.5.3

0.5.3.0 https://github.com/sebastiaanluca/laravel-helpers

A set of Laravel-specific helpers.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel helpers collection form blade pipe

14/02 2017

0.5.2

0.5.2.0 https://github.com/sebastiaanluca/laravel-helpers

A set of Laravel-specific helpers.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel helpers collection form blade pipe

10/02 2017

0.5.1

0.5.1.0 https://github.com/sebastiaanluca/laravel-helpers

A set of Laravel-specific helpers.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel helpers collection form blade pipe

10/02 2017

0.5.0

0.5.0.0 https://github.com/sebastiaanluca/laravel-helpers

A set of Laravel-specific helpers.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel helpers collection form blade pipe

10/02 2017

0.4.0

0.4.0.0 https://github.com/sebastiaanluca/laravel-helpers

A set of Laravel-specific helpers.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel helpers collection form blade pipe

04/02 2017

0.3.1

0.3.1.0 https://github.com/sebastiaanluca/laravel-helpers

A set of Laravel-specific helpers.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel helpers collection form blade pipe

25/01 2017

0.3.0

0.3.0.0 https://github.com/sebastiaanluca/laravel-helpers

A set of Laravel-specific helpers.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel helpers collection form blade pipe

24/01 2017

0.2.0

0.2.0.0 https://github.com/sebastiaanluca/laravel-helpers

A set of Laravel-specific helpers.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel helpers collection form blade pipe

10/01 2017

0.1.0

0.1.0.0 https://github.com/sebastiaanluca/laravel-helpers

A set of Laravel-specific helpers..

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel helpers collection form blade pipe