Ten Helper Functions Recommended by Laravel

  • 2021-12-11 07:14:01
  • OfStack

Preface

Laravel contains various global helper functions. laravel contains a number of helper functions that you can use to simplify your development workflow. Here, I'll write 10 of the best laravel helpers to make my development easier. You must consider using them when necessary.

You can also view all the official documents laravel helper functions.

array_dot()

The array_dot () array_dot () helper function allows you to convert multidimensional arrays to 1-dimensional arrays using dot symbols.


$array = [
 'user' => ['username' => 'something'],
 'app' => ['creator' => ['name' => 'someone'], 'created' => 'today']
];

$dot_array = array_dot($array);

// [user.username] => something, [app.creator.name] => someone, [app.created] => today

array_get()

The array_get () function uses dot symbols to retrieve values from a multidimensional array.


$array = [
 'user' => ['username' => 'something'],
 'app' => ['creator' => ['name' => 'someone'], 'created' => 'today']
];

$name = array_get($array, 'app.creator.name');

// someone

If key does not exist, the array_get () function also accepts an optional third argument as the default value.


$name = array_get($array, 'app.created.name', 'anonymous');

// anonymous

public_path()

public_path () returns the fully qualified absolute path to the public directory in the Laravel application. You can also pass the path to a file or directory in a public directory to get the absolute path of the resource. It will simply add public_path () to your parameters.


$public_path = public_path();

$path = public_path('js/app.js');

Str::orderedUuid()

The Str:: orderedUuid () function first generates a timestamp uuid. This uuid can be stored in index database columns. These uuid are created based on timestamps, so they keep your content index. When used in Laravel 5.6, Ramsey\ Uuid\ Exception\ UnsatisfiedDependencyException is thrown. To fix this problem, simply run the following command to use the moontoast/math package:


composer require "moontoast/math"

use Illuminate\Support\Str;

return (string) Str::orderByUuid()

// A timestamp first uuid

str_plural()

The str_plural function converts a string to a complex form. This function only supports English.


echo str_plural('bank');

// banks

echo str_plural('developer');

// developers

route()

The route () function generates the route URL for the specified route.


$url = route('login');

If the route accepts parameters, you can simply pass them as the second parameter to an array.


$url = route('products', ['id' => 1]);

If you want to generate a relative URL instead of an absolute URL, you can pass false as the third parameter.


$url = route('products', ['id' => 1], false);

tap()

The tap () function takes two arguments: a value and a closure. The value will be passed to the closure, and then the value will be returned. Closure return value is irrelevant.


$array = [
 'user' => ['username' => 'something'],
 'app' => ['creator' => ['name' => 'someone'], 'created' => 'today']
];

$name = array_get($array, 'app.creator.name');

// someone
0

Instead of returning a Boolean value, it returns User Model.

If you don't pass closures, you can also use any of the methods of User Model. Regardless of the actual method returned, the return value will always be a value. In the following example, it returns User Model instead of a Boolean value. The update method returns a Boolean value, but since tap is used, it will return User Model.


$array = [
 'user' => ['username' => 'something'],
 'app' => ['creator' => ['name' => 'someone'], 'created' => 'today']
];

$name = array_get($array, 'app.creator.name');

// someone
1

dump()

The dump () function takes the variables given by dump and also supports passing in multiple variables at the same time. This is very useful for debugging.


$array = [
 'user' => ['username' => 'something'],
 'app' => ['creator' => ['name' => 'someone'], 'created' => 'today']
];

$name = array_get($array, 'app.creator.name');

// someone
2

str_slug()

The str_slug () function generates an URL-friendly slug from the given string. You can use this feature to create an slug for a post or product title.


$slug = str_slug('Helpers in Laravel', '-');

// helpers-in-laravel

optional()

The optional () function accepts 1 parameter, and you can call the method of the parameter or access the property. If the object passed is null, the methods and properties return null instead of causing an error or throwing an exception.


$array = [
 'user' => ['username' => 'something'],
 'app' => ['creator' => ['name' => 'someone'], 'created' => 'today']
];

$name = array_get($array, 'app.creator.name');

// someone
4

Summarize


Related articles: