6
Php

Laravel 3 Helper Functions

Laravel is a clean and classy framework for PHP web development and I’m getting familiar with it and I’m really loving it. Well, Laravel has it’s nice documentation for everything but I didn’t find any documentation on it’s helper functions and I didn’t understand why they didn’t provide any documentation for those really helpful helper functions. Often I use those helper functions to save some code and time so I’ve decided to write about those helper functions so maybe someone else can use those functions without digging in to the code.

These helper functions are located in the laravel/helpers.php file and there are many useful functions but I’ll discuss about some of them, specially all of the array related functions and few other, those I really love to use.


Function : array_get

Description : Returns the specified element from an array using dot notation. This function takes three parameters, the first one is the array from which you want to get the item, the second one is the array key that you want to get and the third parameter is optional that will be returned if the specified is not found.

Example :

Function : array_set

Description : Appends/Sets an item in to the specified array using dot notation. This function takes three parameters, the first one is the array to which you want to set/append the new item, the second one is the array key and the third parameter is the value for that key.

Example :

Function : array_forget

Description : Remove an array item from a given array using “dot” notation. This function takes two parameters, the first one is the array from which you want remove the item and the second one is the array key that you want to remove.

Example :

Function : array_first

Description : Returns the first element in an array which passes a given truth test. This function takes three parameters, the first one is the array from which you want get the first item and the second parameter is a closure (anonymous function) that will check for the given value and will return the first matched array item and the third one is optional that will be returned if specified value is not found.

Example :

Function : array_strip_slashes

Description : Recursively removes slashes from array keys and values and returns a new array. This function takes only one parameter and it is the array that you want to remove slashes from.

Example :

Function : array_divide

Description : Divides an array into two arrays. One with keys and the other with values and returns one new array of two arrays. This function takes only one parameter and it is the array that you want to divide the keys and values in two different arrays.

Example :

Function : array_pluck

Description : Pluck an array of values from an array. This function takes two parameters, the first one is the array that you want to pluck array item from and other one is the array key that you want to pluck and it returns another plucked array of values.

Example :

Function : array_only

Description : Get a subset of the items from the given array. This function takes two parameters one is the array, from which you want to get another subset of arrays and the other one is the array keys, those you want to apart and get as a subset of the original array.

Example :

Function : array_except

Description : Get all of the given array except for a specified array of items. This function takes two parameters one is the array, from which you want to get array items and the other one is the array keys, those you don’t want to get from the original array, just opposite of the array_only function.

Example :

Function : head

Description : Returns the first element of an array. This function takes only one parameter and that is the array, from which you want to retrieve the first element. This is simply a convenient wrapper around the reset method, you can do the same thing using echo reset($array);.

Example :

Function : eloquent_to_json

Description : Transforms Eloquent models to a JSON object. This function takes only one parameter and that is the Eloquent|array model, which you want to transform into a JSON object.

Example :

Function : starts_with

Description : Determines if a given string begins with a given value. This function takes two parameters, first one is the string, in which you want to look up the string/value and the second one is the value, which you want to check if it is at the beginning of the given string.

Example :

Function : ends_with

Description : Determine if a given string ends with a given value. This function takes two parameters, first one is the string, in which you want to look up the string/value and the second one is the value, which you want to check if it is at the end of the given string, just opposite of the starts_with function.

Example :

Function : str_contains

Description : Determines if a given string contains a given sub-string. This function takes two parameters, first one is the string, in which you want to look up the sub-string and the second one is the sub-string, which you want to check if it is available in the given string.

Example :

Function : str_finish

Description : Cap/Adds a string with a single instance of the given string. This function takes two parameters, first one is the string, in which you want to cap/add another string at the end and the second one is the string, which you want to add at the end of the main/first string.

Example :

Function : get_file_size

Description : Calculate the human-readable file size (with proper units). This function takes only one parameter and it’s the int value of bytes and it outputs the human readable format of the size using proper units, for example, if you give the parameter 1024 (as byte) then it’ll output 1KiB.

Example :

There are other useful and very handy helper functions available in Laravel that we can use often and these are:

Also another handy and most useful helper function in Laravel is dd($value); which is very useful for debugging the code, it is a two in one function because it works as dump and die. There is no need to explain about this function, I believe every Laravel developer use this function more than any other function during the development of any project. I hope, this writing will be helpful to other and also it would be a good idea to dig in to the source code, reading the source code will definitely help us to learn new tips and tricks, it doesn’t matter how expert you are, you will get new ideas.

Latest Blog