• This repository has been archived on 06/Feb/2023
  • Stars
    star
    418
  • Rank 103,380 (Top 3 %)
  • Language
    PHP
  • License
    MIT License
  • Created about 8 years ago
  • Updated almost 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

THIS PACKAGE HAS BEEN DEPRECATED — An extensive set of Laravel framework helper functions and collection macros.

Latest stable release Software license Build status Total downloads Total downloads

Read my blog View my other packages and projects Follow @sebastiaanluca on Twitter Share this package on Twitter

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

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:

composer require sebastiaanluca/laravel-helpers

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

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.

See the changelog for more information.

Framework helper functions

locale

Get the active app locale or the fallback locale if it's missing or not set.

locale();

// "en"

is_guest

Determine if the current user is a guest.

The opposite of is_logged_in.

// When not authenticated
is_guest();

// true

// When authenticated as a user
is_guest();

// false

is_logged_in

Determine if the current user is authenticated.

The opposite of is_guest.

// 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).

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

// 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).

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

An alternative for user.

// 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.

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.

The second parameter is optional and falls back to the start string if null.

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

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

transformKeys

Perform an operation on the collection's keys.

The callable operation can either be a globally available method or a closure.

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.

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.

Before:

id name
A 1 James
B 2 Joe
C 3 Jonas

After:

A B C
id 1 2 3
name James Joe Jonas

How to use:

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.

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.

Explicitly requires the kint-php/kint package.

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.

Explicitly requires the kint-php/kint package.

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.

Change log

Please see CHANGELOG for more information what has changed recently.

Testing

composer install
composer test

Contributing

Please see CONTRIBUTING and CODE OF CONDUCT for details.

Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

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.

Have a project that could use some guidance? Send me an e-mail at [email protected]!

More Repositories

1

php-pipe-operator

Method chaining for any value using any method.
PHP
280
star
2

laravel-router

THIS PACKAGE HAS BEEN DEPRECATED — An organized approach to handling routes in Laravel.
PHP
129
star
3

laravel-module-loader

THIS PACKAGE HAS BEEN DEPRECATED — A lightweight package to organize your code into contextual modules.
PHP
77
star
4

laravel-auto-morph-map

THIS PACKAGE HAS BEEN DEPRECATED — Automatically alias and map the polymorphic types of Eloquent models.
PHP
54
star
5

laravel-boolean-dates

Automatically convert Eloquent model boolean attributes to dates (and back).
PHP
35
star
6

php-helpers

THIS PACKAGE HAS BEEN DEPRECATED — An extensive set of PHP helper functions and classes.
PHP
27
star
7

laravel-4-eventcron

THIS PACKAGE HAS BEEN DEPRECATED — Laravel 4.x database event queue
PHP
27
star
8

laravel-conditional-providers

THIS PACKAGE HAS BEEN DEPRECATED — Load Laravel service providers and facades based on the current environment.
PHP
26
star
9

laravel-skeleton-deprecated-2

THIS PACKAGE HAS BEEN DEPRECATED — A ready-to-go Laravel skeleton application.
PHP
21
star
10

laravel-route-model-autobinding

THIS PACKAGE HAS BEEN DEPRECATED — Automatically bind Eloquent models as route segment variables.
PHP
14
star
11

php-stub-generator

THIS PACKAGE HAS BEEN DEPRECATED — Easily generate files and classes from stubs.
PHP
10
star
12

laravel-unbreakable-migrations

THIS PACKAGE HAS BEEN DEPRECATED — Prevent your Laravel database migrations from failing by wrapping them in transactions.
PHP
9
star
13

laravel-blog

THIS PACKAGE HAS BEEN DEPRECATED — A simple-to-use blog system you can pull into any existing Laravel project and immediately get writing.
PHP
5
star
14

php-codesniffer-ruleset

THIS PACKAGE HAS BEEN DEPRECATED — An opinionated custom coding standard
4
star
15

laravel-changelog

THIS PACKAGE HAS BEEN DEPRECATED — Show your project's changelog in your application.
PHP
4
star
16

laravel-goodies

THIS PACKAGE HAS BEEN DEPRECATED — A curated list of Laravel packages and other goodies.
3
star
17

laravel-validator

THIS PACKAGE HAS BEEN DEPRECATED — Properly handle user input by using your validation rules as source of truth
PHP
3
star
18

laravel-presets

THIS PACKAGE HAS BEEN DEPRECATED — Various plug-and-play presets to scaffold your existing project.
PHP
3
star
19

Gmail-Tracker-Extension

THIS PACKAGE HAS BEEN DEPRECATED — Chrome extension for Gmail to enable tracking emails.
JavaScript
2
star
20

alarmclock

NodeJS alarm clock for the Raspberry Pi.
JavaScript
2
star
21

laravel-skeleton

An opinionated fresh Laravel project to help you get started.
PHP
1
star
22

laravel-resource-flow

THIS PACKAGE HAS BEEN DEPRECATED — Quickly scaffold access to a resource.
PHP
1
star
23

P5RuntimeFullScreen

THIS PACKAGE HAS BEEN DEPRECATED — Fullscreen mode for Processing that actually lets you switch (on the fly) between a windowed and a fullscreen sketch. With support for Processing 2 (beta), customisable hotkeys, OpenGL, Proscene, Eclipse, …
Java
1
star