11 Awesome Laravel Helper Functions (that aren't in Laravel)

TLDR: I packaged up all the helper functions I use in my projects. They are cool. You can install it by running composer require calebporzio/awesome-helpers.

Let’s just jump right in and introduce the helper functions I included in the package and find uber-duber useful.

We’ll go in ascending order of awesome-ness, so you’re not overwhelmed with too much 🔥out of the gate.

ok()

For those times you just want to return a successful response from a controller without any data. However, you think return response() is either ugly, not semantic, or plain wrong (because it returns a 200 by default and the 204 status code (No Content) is more accurate).

Route::get('/', function () {  
    return ok();
);

Thanks to Logan Henson for the idea.

str_wrap()

str_wrap('pigs', '-blanket-');  
// returns: "-blanket-pigs-blanket-"

str_between()

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

str_match()

Because every time I have to write $matches[1]using preg_match, I die a little inside.

str_match('persianwifefinder.com', '/(.*)\.com/');  
// returns "persianwifefinder"

str_validate()

A nice, simple way to harness the power of Laravel’s validator without writing something heavy like Validator::make(…), having to name the data you want to validate, and potentially dealing with a validation exception when all you want is a message or two.

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

carbon()

If I had a nickel for every time I saw Error: Class ‘…\Carbon' not found because I forgot to import Carbon\Carbon, I could attend Laracon Online.

carbon('1 year ago');  
// Same as:
Carbon::parse('1 year ago');  

user()

Writing auth()-> before every user() is 8 characters of my apps I will never get back.

user();  
// Same as:
auth()->user();  

money()

If you’ve every stumbled on PHP’s money_format, function and thought you found a nice simple tool for converting ints, to dollar strings, and been fooled again? This one’s for you.

money(12); // returns "$12.00"  
money(12.75); // returns "$12.75"  
money(12.75, $withCents = false); // returns "$13"  

stopwatch()

For debugging and profiling random stuff without looking on stack overflow to figure out how to get something sensible out of microtime(). stopwatch() is a simple way to time (in seconds) whatever code you run inside a callback.

stopwatch(function () {  
    sleep(4);
});
// returns 4.000....

chain()

Why every class ever isn’t fluent… I just don’t know. chain() allows you to make any ole’ PHP object chainable (meaning you can chain methods one after the other). There is also a crazy handy feature added to this (the “{carry}” operator, for passing the result of one method into the next).

chain(new Str)  
    ->singular('cars')
    ->upper('{carry}')
    // If "{carry}" isn't Z enough for you, Taylor had the nify idea to make a "carry" constant available (see next line) with the function. The choice is yours.
    ->start(carry, 'the ')
  ->finish(carry, ' life!')();

// returns "the CAR life"
// Note: the "()" at the end get's the result returned from the last called method. You can also just cast the result to a string `(string)` if the result is a string.

tinker()

And now, for the most 🔥helper of them all (I have a tweet to prove it). A helper function so useful, it just may be in the same league as the infamous dd(). I give you…

$user = User::find($id);

tinker($user);  
// Will open up an `artisan tinker` session with the `$user` object available for manipulation.
// Note: only works when run inside a console context, most commonly this will be your PHPUnit tests.

What a ride…

Well there you go. 11 super-dee-duper useful helper functions to improve your Laravel workflow.

If you have some of your own you want to contribute to the package, you can submit PRs here.

TTFN,
Caleb