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:

$converted = camel_case('foo_bar');
// fooBar

kebab_case()

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

$converted = kebab_case('fooBar');
// foo-bar

str_slug()

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

$slug = str_slug('Laravel 5 Framework', '-');
// laravel-5-framework

studly_case()

The studly_case function converts the given string to StudlyCase:

$converted = studly_case('foo_bar');
// FooBar

title_case()

The title_case function converts the given string to Title Case:

$converted = title_case('a nice title uses the correct case');
// A Nice Title Uses The Correct Case

preg_replace_array()

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

$string = 'The event will take place between :start and :end';
$replaced = preg_replace_array('/:[a-z_]+/', ['8:30', '9:00'], $string);
// The event will take place between 8:30 and 9:00

snake_case()

The snake_case function converts the given string to snake_case:

$converted = snake_case('fooBar');
// foo_bar

starts_with()

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

$result = starts_with('This is my name', 'This');
// true

ends_with()

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

$result = ends_with('This is my name', 'name');
// true

str_limit()

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

$truncated = str_limit('The quick brown fox jumps over the lazy dog', 20);

str_plural()
The str_plural function converts a string to its plural form. This function currently only supports the English language:
$plural = str_plural('car');
// cars

$plural = str_plural('child');

// children
You may provide an integer as a second argument to the function to retrieve the singular or plural form of the string:

$plural = str_plural('child', 2);

// children

$plural = str_plural('child', 1);

// child

str_singular()

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

$singular = str_singular('cars');

// car

$singular = str_singular('children');

// child

str_random()

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

$random = str_random(40);

asset()

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

$url = asset('img/photo.jpg');

auth()

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

$user = auth()->user();

If needed, you may specify which guard instance you would like to access:
$user = auth('admin')->user();

back()

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

return back($status = 302, $headers = [], $fallback = false);
return back();

bcrypt()

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

$password = bcrypt('my-secret-password');

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_field() }}

csrf_token()

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

$token = csrf_token();

dd()

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

dd($value);

dd($value1, $value2, $value3, ...);

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:

dump($value);

dump($value1, $value2, $value3, ...);

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”:

filled(0);
filled(true);
filled(false);

// true

filled('');
filled(' ');
filled(null);
filled(collect());

// false

blank()

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

blank('');
blank(' ');
blank(null);
blank(collect());

// true

blank(0);
blank(true);
blank(false);

// false

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:

return redirect($to = null, $status = 302, $headers = [], $secure = null);
return redirect('/home');
return redirect()->route('route.name');

request()

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

$request = request();
$value = request('key', $default);

session()

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

$value = session('key');
You may set values by passing an array of key / value pairs to the function:

session(['chairs' => 7, 'instruments' => 3]);
The session store will be returned if no value is passed to the function:

$value = session()->get('key');

session()->put('key', $value);

Leave a Reply

Your email address will not be published.