8 Laravel Helpers I Use Everyday

Here is the list of my favorite Laravel helper function which saves my time a lot.

8 Laravel Helpers I Use Everyday

Laravel provides a ton of helper functions which can be very useful to accomplish basic tasks. You can check out all Laravel helper functions on Laravel Official Documentation webpage.

In this post, I will be listing some of the best Laravel helpers functions which I use every day and make my developer easier. You should consider using them and get a grip on them.

Laravel 6 Update

All string and array helper functions in Laravel 6 has been moved to a separate package. To use the string and array helper functions you need to install laravel/helpers package separately. Run this command in the command line terminal to install this package. composer require laravel/helpers

1. route()

The route() function is very helpful to generate a url for named routes. I mostly use them when creating navigation menus or in form’s action parameter. You can generate url of a named route like below.

$url = route('home');

If you have a named route with parameters then you can pass the second argument as array of parameters values. Like so:

$url = route('product.show', ['slug' => 'apple-iphone']);

Above code will generate an absolute URL for the named route but you can pass third parameter flase to generate a relative URL. By default, this function will generate an absolute URL.

$url = route('product.show', ['slug' => 'apple-iphone'], false); 
// will generate a relative url

2. tap()

The tap() Laravel helpers function is one of my favorite. It takes two arguments, a value, and a closure. The value will be passed to the closure and the same value will be returned back.

$product = Product::find(1);
return tap($product, function ($product) {
        'price' =>  '200'

Above code snippet will return the $product model instead of a boolean value.

You can also chain any methods on this function. The value will always be return regardless of what you perform in the method.

$product = Product::find(1);
return tap($product)->update([
    'price' =>  '200'

In the example above, it will return the product model instead of boolean value. Update method on product model will return boolean but because of tap function, it will return the model object.

3. dump() and dd()

The dump() and dd() are very useul for debugging your code. The dump function dumps the given variable without stopping the execution of your code. While dd will dump the variable and stop the execution of application.


4. str_slug()

The str_slug() function generate a URL friendly slug of a given string. It’s very useful to generate the slugs for blog posts or products.

$slug = str_slug('8 Laravel Helpers I Use Everyday', '-');
// 8-laravel-helpers-i-use-everyday

5. optional()

The optional() Laravel helpers function takes an argument and you can call the properties or methods on that argument. If the argument passed is null, then properties or methods will return null instead of throwing horrible errors.

$product = Product::find(1);
return optional($product)->weight;

6. array_pluck()

Dealing with arrays is always fun for me. The array_pluck() function return, all values of an array for a given key.

$products = [
    ['tablets'   =>
        ['id' => '1', 'name' => 'Amazon Fire HD', 'price' => '99.99'],
        ['id' => '2', 'name' => 'Samsung Galaxy', 'price' => '149.99'],
        ['id' => '3', 'name' => 'Apple iPad 32GB', 'price' => '499.99'],
    ['laptops'   =>
        ['id' => '11', 'name' => 'Lenovo ThinkPad', 'price' => '299.99'],
        ['id' => '12', 'name' => 'Asus ZenBook', 'price' => '699.99'],
        ['id' => '13', 'name' => 'Microsoft Surface Pro', 'price' => '379.99'],

$laptopNames = array_pluck($products, 'laptops.name');

// ['Lenovo ThinkPad', 'Asus ZenBook', 'Microsoft Surface Pro']

7. asset()

The asset() Laravel helpers function return the url of an asset using the current URL schema like http or https.

$cssUrl = asset('css/style.css');
// https://www.larashout.com/css/style.css

8. config()

The config() function take the configuration varibale and return its value.

$app = config('app.name');
// LaraShout

I hope you have enjoyed this short post about Laravel helpers functions. You can find all helper function on the Laravel documentation site, but I shared the ones I use most.

Your little help will keep this site alive and help us to produce quality content for you.