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

Spread the word! Share with your fellow developers.

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

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

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() function is one of my favorite. It takes two arguments, a value and a closure. The value will be passed to the closure and same value will be returned back.

$product = Product::find(1);
return tap($product, function ($product) {
    $product->update([
        '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.

dump($product);
dd($product);

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() function take 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() 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. You can find all helper function on the Laravel documentation site, but I shared the ones I use most.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

*
*

This site uses Akismet to reduce spam. Learn how your comment data is processed.