GitHub - calebporzio/awesome-helpers: Helper functions I find super-duper handy
source link: https://github.com/calebporzio/awesome-helpers
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
README.md
A collection of awesome helpful functions for Laravel
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)
me
A shortcut for auth()->user()
user()->posts()->create([...]);
money
echo money(12); // echoes "$12.00" echo money(12.75); // echoes "$12.75" // Note: will detect current locale, and use appropriate formating.
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
str_between('--thing--', '--'); // returns "thing" str_between('[thing]', '[', ']'); // returns "thing"
str_match
Because preg_match
is annoying.
str_match('Jan-1-2019', '/Jan-(.**)-2019/'); // returns "1"
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_wrap
str_wrap('thing', '--'); // returns "--thing--"
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
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK