Laravel Helpers – Global Functions For Laravel Developers

Laravel is a trending framework for PHP nowadays. The popularity of Laravel is increasing day by day. There are several reasons behind this popularity – clean code, template engine, artisan commands, migrations, security, etc.

In this article, we discuss one more feature provided which is Laravel Helpers.

Laravel Helpers are global functions developers should use in their application. Laravel itself uses those functions in a framework.

Why Should Use Laravel Helpers?

For developing a website, we either choose CMS or Framework. While writing a code for the application, it is a standard practice to use methods provided by CMS or Framework.

As an example, if we are using a WordPress then for inserting data in the database table we write a code something like below.

global $wpdb;
        'column1' => 'value1', 
        'column2' => 123 

We never or should not write mysql insert query like INSERT INTO table(column1,..) VALUES(value1,..) in WordPress.

This rule applies to all CMS and Frameworks.

Laravel Helpers are also the methods which developers should use while writing a code in Laravel.

Laravel Helpers

What Are the Methods?

Well, there are several global functions includes in a helpers. You can read about Laravel Helpers on their official documentation. We discuss some of them below.

array_add(): The array_add function adds a given key / value pair to the array if the given key doesn’t already exist in the array.

$array = ['first_name' => 'Sajid', 'last_name' => 'Sayyad'];
$array = array_add($array, 'email', '');

//output: ['first_name' => 'Sajid', 'last_name' => 'Sayyad', 'email' => ''];

array_divide(): The array_divide function returns two arrays, one containing the keys, and the other containing the values of the original array. You can use this one in place of array_keys() and array_values() functions.

list($keys, $values) = array_divide($array);

//$keys: ['first_name', 'last_name', 'email']
//$values: ['Sajid', 'Sayyad', '']

ends_with(): The ends_with function determines if the given string ends with the given value.

$value = ends_with('This is my blog', 'blog');

//Output: true

str_limit(): The str_limit function limits the number of characters in a string. The function accepts a string as its first argument and the maximum number of resulting characters as its second argument.

$value = str_limit('I am a PHP Developer.', 7);
echo $value;

//Output: I am a...

str_contains(): The str_contains function determines if the given string contains the given value.

$value = str_contains('This is my name', 'my');

//Output: true

str_random(): The str_random function generates a random string of the specified length. This function uses PHP’s random_bytes function.

$string = str_random(40);

//Output: random string of 40 characters length

bcrypt(): The bcrypt function hashes the given value using Bcrypt.

$password = bcrypt('123456');

//Ouput: hash password

These are some of the helper methods of a Laravel. Read the Laravel documentation for more functions available for paths, URLs, arrays, strings.

Related Articles

If you liked this article, then please subscribe to our YouTube Channel for video tutorials.

Leave a Reply

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