Home >PHP Framework >Laravel >Ten recommended Laravel helper functions

Ten recommended Laravel helper functions

步履不停
步履不停Original
2019-06-19 11:28:402887browse

Ten recommended Laravel helper functions

Laravel includes various global helper functions. Laravel includes a number of helper functions that you can use to streamline your development workflow. Here I will write 10 best laravel helper functions that I use to make my development easier. You must consider using them when necessary.

You can also view all official documentationlaravel helper functions.

array_dot()

array_dot() array_dot( ) helper function allows you to convert a multidimensional array to a one-dimensional array using dot notation.

$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()

array_get() The function retrieves values ​​from a multidimensional array using dot notation.

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

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

// someone

If the key does not exist, the array_get() function also accepts an optional third parameter as a 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 your Laravel application. You can also pass the path to a file or directory in the public directory to get the absolute path to the resource. It will simply add public_path() to your parameters.

$public_path = public_path();

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

Str::orderedUuid()

Str::orderedUuid() The function first generates a timestamp uuid. This uuid can be stored in an indexed database column. These UUIDs are created based on timestamps, so they keep your content indexed. When using this in Laravel 5.6, Ramsey\Uuid\Exception\UnsatisfiedDependencyException is thrown. To solve this problem, just 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()

str_plural The function will String converted to plural form. This feature only supports English.

echo str_plural('bank');

// banks

echo str_plural('developer');

// developers

route()

route() The 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()

tap() The function accepts two parameters: a value and a closure. The value will be passed to the closure and the value will be returned. The closure return value doesn't matter.

$user = App\User::find(1);

return tap($user, function($user) {
    $user->update([
        'name' => 'Random'
    ]);
});

It does not return a Boolean value, but User Model.

If you don't pass a closure, you can also use any method of User Model. The return value will always be a value regardless of the method that actually returns it. In the example below, it will return User Model instead of a boolean value. The update method returns a Boolean value, but since tap is used, it will return User Model.

$user = App\User::find(1);

return tap($user)->update([
    'name' => 'SomeName'
]);

dump()

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

dump($var1);
dump($var1, $var2, $var3);

str_slug()

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

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

// helpers-in-laravel

optional()

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

$user = User::find(1);

return optional($user)->name;

For more Laravel related technical articles, please visit the Laravel Tutorial column to learn!

The above is the detailed content of Ten recommended Laravel helper functions. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn