Speed up Development With These Laravel Helpers

Laravel is the most widely used frameworks these days as it provides developers with a lot of easy ways in development and provides many many helpers and functions and below we illustrate 5 of these helpers.

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.

If you use a “wildcard” (*) in your dot notation, Laravel will return an array of results.

The data_get() helper allows you to find elements in your arrays and objects using the same syntax, which is excellent since you don’t have to check what type of variable you have before using it.

 

str_plural()

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.

The str_plural()helper will allow you to remove code similar to this: {{ $count == 1 ? 'dog' : 'dogs' }}. There is also a str_singular() helper available. If you’re interested in learning more about how this works, most of the heavy lifting is done by Doctrine’s Inflector Class.

 

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.

By passing false to the optional third parameter, you can have the helper return a relative URL instead of an absolute one.

Routing for sub-domains works as straightforward as adding another attribute

You can also pass an Eloquent model directly to route()

This works because the base Model class implements the UrlRoutable interface. The default behavior uses the primary key of the model, but you can override this by adding a getRouteKeyName() method to your model.

Then your usage would become



abort_if()

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.

So many of us have done something similar to the below example, and the abort_if() helper can cut it down to one line.

Note: If you’re strictly doing this check access control, you should take a peek at using authorization gates. Once you have the gate in place you can then wrap your admin and customer specific routes, so you don’t need to have abort checks in your codebase – or at least not as many.

 

optional()

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.

 

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.

Go to laravel website to take a look at all of the available helpers.

Share this: