Caleb Porzio

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

Feb 2019

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.


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('pigs', '-blanket-');
// returns: "-blanket-pigs-blanket-"


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


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

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


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('[email protected]', 'regex:/\.net$/|email|max:10');
// returns: ["Format is invalid.", "May not be greater than 10 characters."]


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');


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

// Same as:


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"


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 () {
// returns 4.000....


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


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);

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

My Newsletter

I send out an email every so often about cool stuff I'm working on or launching. If you dig, go ahead and sign up!