Useful functions in Laravel and its Usage explained

There are many useful “helper” PHP functions in Laravel. According to documentation, “Many of these functions are used by the framework itself; however, you are free to use them in your own applications if you find them convenient.”

We are listing some of many useful functions Laravel has built in. You may check this page for a complete list.

camel_case()

The camel_case function converts the given string to camelCase:

kebab_case()

The kebab_case function converts the given string to kebab-case:

str_slug()

The str_slug function generates a URL friendly “slug” from the given string:

studly_case()

The studly_case function converts the given string to StudlyCase:

title_case()

The title_case function converts the given string to Title Case:

preg_replace_array()

The preg_replace_array function replaces a given pattern in the string sequentially using an array:

snake_case()

The snake_case function converts the given string to snake_case:

starts_with()

The starts_with function determines if the given string begins with the given value:

ends_with()

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

str_limit()

The str_limit function truncates the given string at the specified length:

str_singular()

The str_singular function converts a string to its singular form. This function currently only supports the English language:

str_random()

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

asset()

The asset function generates a URL for an asset using the current scheme of the request (HTTP or HTTPS):

auth()

The auth function returns an authenticator instance. You may use it instead of the Auth facade for convenience:

If needed, you may specify which guard instance you would like to access:

back()

The back function generates a redirect HTTP response to the user’s previous location:

bcrypt()

The bcrypt function hashes the given value using Bcrypt. You may use it as an alternative to the Hash facade:

csrf_field()

The csrf_field function generates an HTML hidden input field containing the value of the CSRF token. For example, using Blade syntax:

csrf_token()

The csrf_token function retrieves the value of the current CSRF token:

dd()

The dd function dumps the given variables and ends execution of the script:

If you do not want to halt the execution of your script, use the dump function instead.

dump()

The dump function dumps the given variables:

If you want to stop executing the script after dumping the variables, use the dd function instead.

filled()

The filled function returns whether the given value is not “blank”:

blank()

The blank function returns whether the given value is “blank”:

For the inverse of blank, see the filled method.

redirect()

The redirect function returns a redirect HTTP response, or returns the redirector instance if called with no arguments:

request()

The request function returns the current request instance or obtains an input item:

session()

The session function may be used to get or set session values:

Leave a Reply