• This repository has been archived on 21/Jan/2024
  • Stars
    star
    629
  • Rank 71,454 (Top 2 %)
  • Language
    PHP
  • Created almost 6 years ago
  • Updated over 3 years ago

Reviews

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

Repository Details

Helper functions I find super-duper handy

A collection of awesome helpful functions for Laravel

Travis CI status image

Installation

composer require calebporzio/awesome-helpers

Helpers

carbon

Shortcut for: new Carbon or Carbon::parse()

carbon('One year ago');

chain

Makes an ordinary object chainable.

chain(new SomeClass)
    ->firstMethod()
    ->secondMethod()
    ->thirdMethod();

// You can use the "carry" constant to pass the result of one method into the other:
chain(new Str)->singular('cars')->ucfirst(carry)();
// Returns "Car"
// Also, you can grab the result of the chain by trailing
// a "()" on the end of it. (Thanks to Taylor Otwell for these two additions)

connection

Run callback under a different database connection.

$tenantPostIds = connection('tenantdb', function () {
    return Post::pluck('id');
});

dump_sql

Returns sql query with bindings data.

dump_sql(\DB::table('users')->where('email', "blaBla")->where('id', 1));
// returns "select * from `users` where `email` = 'blaBla' and `id` = 1"

faker

Shortcut for: $faker = Faker\Factory::create()

faker()->address; // returns random, fake address
faker('address'); // alternate syntax

user

A shortcut for auth()->user()

user()->posts()->create([...]);

money

echo money(12); // echoes "$12.00"
echo money(12.75); // echoes "$12.75"
echo money(12.75, false); // echos "$13"
echo money(12.75, true, 'en_GB'); // echos "Β£12.75"
// Note: unless specified otherwise, money() will detect the current locale.

ok

Shortcut for response('', 204). When you don't have anything to return from an endpoint, but you want to return success.

return ok();

stopwatch

Returns the amount of time (in seconds) the provided callback took to execute. Useful for debugging and profiling.

stopwatch(function () {
    sleep(2);
}); // returns "2.0"

str_between

Returns string between second argument

str_between('--thing--', '--'); // returns "thing"
str_between('[thing]', '[', ']'); // returns "thing"

Str::between('--thing--', '--'); // returns "thing"
Str::between('[thing]', '[', ']'); // returns "thing"

str_extract

Returns capture groups contained in the provided regex pattern.

str_extract('Jan-01-2019', '/Jan-(.*)-2019/'); // returns "01"

Str::extract('Jan-01-2019', '/Jan-(.*)-2019/'); // returns "01"

str_match

Checks the provided string against the provided regex pattern.

str_match('Jan-01-2019', '/Jan-.*-2019/'); // returns true
str_match('foo bar baz', 'bar'); // returns true

Str::match('Jan-1-2019', '/Jan-(.*)-2019/'); // returns true

str_validate

A simple way to use validate a string using Laravel's built-in validation system.

str_validate('[email protected]', 'regex:/\.net$/|email|max:10');
// returns: ["Format is invalid.", "May not be greater than 10 characters."]

Str::validate('[email protected]', 'regex:/\.net$/|email|max:10');
// returns: ["Format is invalid.", "May not be greater than 10 characters."]

str_wrap

str_wrap('thing', '--'); // returns "--thing--"

Str::wrap('thing', '--'); // returns "--thing--"

swap

This function swaps the values of two variables.

$startDate = '2040-01-01';
$endDate = '2020-01-01';

if ($endDate < $startDate) {
    swap($startDate, $endDate);
}

echo $startDate; // prints "2020-01-01"
echo $endDate; // prints "2040-01-01"

tinker

Kind of like dd(), but will open an artisan tinker terminal session with the variables you passed in, so you can play around.

$somethingYouWantToDebug = new StdClass;
tinker($somethingYouWantToDebug);

Am I missing an awesome helper function?

Submit a PR or issue with helper functions you use or ideas you have for others!

TTFN, Caleb

More Repositories

1

sushi

Eloquent's missing "array" driver.
PHP
2,570
star
2

onboard

A Laravel package to help track user onboarding steps.
PHP
439
star
3

gitdown

A simple package to parse Github Flavored Markdown in PHP
PHP
219
star
4

better-phpunit

A better PHPUnit test runner for VS Code
TypeScript
211
star
5

laracasts-livewire-datatable

The Laravel project I used during the "Building DataTables with Livewire" Laracasts video
PHP
118
star
6

bear-sync

Access your Bear notes in Laravel
PHP
107
star
7

tailbuild

A simple command to build a Tailwind CSS file for your project (with JIT compiling and watching)
JavaScript
89
star
8

laravel-helpers-file

Because I can never remember exactly how to autoload my helpers.php file.
PHP
61
star
9

laravel-frontend-preset

My personal frontend preset for new laravel applications.
PHP
31
star
10

laracon-online-2020

The Laravel project used in my Laracon Online 2020 talk
PHP
28
star
11

laracasts-building-alpine

JavaScript
27
star
12

simple-php-cs-fixer

A VS Code extension for simple php-cs-fixer integration
PHP
24
star
13

sps

TypeScript
20
star
14

vue-form-state

JavaScript
19
star
15

vercel-laravel

PHP
19
star
16

click

The podcast recording and editing suite of your dreams. Featuring just the one button.
Crystal
18
star
17

livewire-uncovered

PHP
13
star
18

laracasts-turbolinks

The source app from my Laracasts turbolinks video.
PHP
11
star
19

usesushi.dev

The website for sushi
PHP
10
star
20

presettings

A VS Code extension for storing and activating settings presets
TypeScript
10
star
21

laracasts-server-fetched-partials

The Laravel app used in my Laracasts episode on "Server-Fetched Partials"
PHP
10
star
22

laracasts-cached-server-fetched-partials

The finished Laravel app from my "Caching Server Fetched Partials" video.
PHP
10
star
23

vue-fetch-html

A little component for fetching html/vue/javascript from the server.
Vue
9
star
24

livewire-bot

Livewire's GitHub bot
PHP
7
star
25

laravel-acceptance-example

An example laravel project with acceptance tests that render javascript and work well with laravel's helpers.
PHP
7
star
26

model-inheritance

PHP
6
star
27

99bottles-php

PHP
4
star
28

edoc-2019

PHP
4
star
29

vue-example-component

For when you regret getting rid of the default Laravel ExampleComponent.vue
Vue
3
star
30

screendit

PHP
3
star
31

calebporzio

3
star
32

johnny

HTML
2
star
33

dotfiles

Shell
2
star
34

write-less-js

PHP
1
star
35

advent-of-code-2018

My Advent Of Code solutions for 2018
PHP
1
star
36

rm-me

1
star
37

spend-mo

JavaScript
1
star
38

psrpoetry

HTML
1
star
39

ingenuity

CSS
1
star
40

ingenuity-teaser

HTML
1
star
41

theretireddevtheme

A modified ghost (blog) theme for theretireddev.com
CSS
1
star