There are a ton of helper methods in Laravel that make development more efficient. If you work with the framework, I encourage you to see what helpers you can introduce in your day-to-day work. In this blog post, I’d like to point out a few of my favorites.
data_get
The data_get() helper allows you to get a value from an array or object with dot notation. This functions similarly to array_get() as well. The optional third parameter can be used to supply a default value if the key is not found.
The str_plural() helper converts a string to its plural form. Currently, only English is supported. The optional second parameter will allow the helper to choose the plural or singular form. The helper is also smart enough to help with “uncountable” or special case words.
str_plural('child'); // children str_plural('person'); // people str_plural('fish'); // fish str_plural('deer', 2); // deer
route
The route() helper generates a URL for the specified named route. The optional second argument will accept additional route parameters. If additional parameters aren’t named Laravel will try it’s best to match them to the attributes on the route then will add any remaining parameters to the end of the URL.
The abort_if() helper throws an exception if the given expression evaluates to true. The optional third parameter will accept a custom response text, and the optional fourth argument will accept an array of headers.
1 2 3
abort_if(! Auth::user()->isAdmin(), 403); abort_if(! Auth::user()->isAdmin(), 403, 'Sorry, you are not an admin'); abort_if(Auth::user()->isCustomer(), 403);
So many of us have done something similar to the below example, and the abort_if() helper can cut it down to one line.
1 2 3 4 5 6 7 8 9 10 11 12 13
// In "admin" specific controller publicfunctionindex() { if (! Auth::user()->isAdmin()) { abort(403, 'Sorry, you are not an admin'); } }
The optional() helper allows you to access properties or call methods on an object. If the given object is null, properties and methods will return null instead of causing an error.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// User 1 exists, with account $user1 = User::find(1); $accountId = $user1->account->id; // 123
// User 2 exists, without account $user2 = User::find(2); $accountId = $user2->account->id; // PHP Error: Trying to get property of non-object
// Fix with optional() $accountId = optional($user2->account)->id; // null
The optional() helper is ideal when using objects you might not own or calling nested data within Eloquent relationships that may, or may not, be available.
npm ERR! code ELIFECYCLE npm ERR! errno 2 npm ERR! @ development: `cross-env NODE_ENV=development node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js` npm ERR! Exit status 2 npm ERR! npm ERR! Failed at the @ development script. npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log ofthis run can be found in: npm ERR! C:\Users\Administrator\AppData\Roaming\npm-cache\_logs\2018-02-23T02_05_24_275Z-debug.log npm ERR! code ELIFECYCLE npm ERR! errno 2 npm ERR! @ dev: `npm run development` npm ERR! Exit status 2 npm ERR! npm ERR! Failed at the @ dev script. npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log ofthis run can be found in: npm ERR! C:\Users\Administrator\AppData\Roaming\npm-cache\_logs\2018-02-23T02_05_24_301Z-debug.log