Home> PHP Framework> Laravel> body text

Ten recommended Laravel helper functions

步履不停
Release: 2019-06-27 18:05:57
Original
2651 people have browsed it

Ten recommended Laravel helper functions

array_dot()

function allows you to convert a multidimensional array into 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
Copy after login

array_get()

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
Copy after login
The array_get() function also accepts an optional third parameter as a default value if key does not exist.
$name = array_get($array, 'app.created.name', 'anonymous'); // anonymous
Copy after login

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');
Copy after login

Str::orderedUuid()

(1) 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;
(2) When using this in Laravel 5.6, a Ramsey\Uuid\Exception\UnsatisfiedDependencyException is thrown. To resolve this issue, just run the following command to use the moontoast/math package
composer require laravel/passport=~7.0
use Illuminate\Support\Str; return (string) Str::orderByUuid() // A timestamp first uuid
Copy after login

str_plural()

Convert a string to plural form. This feature only supports English.
echo str_plural('bank'); // banks echo str_plural('developer'); // developers
Copy after login

route()

Generate the route URL for the specified route.
$url = route('login'); // 如果路由接受参数,你可以简单地将它们作为第二个参数传递给一个数组。 $url = route('products', ['id' => 1]); // 如果你想产生一个相对的 URL 而不是一个绝对的 URL,你可以传递 false 作为第三个参数。 $url = route('products', ['id' => 1], false);
Copy after login

#tap()

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' ]); }); /** * 它不会返回布尔值,而是返回 User Model 。如果你没有传递闭包,你也可以使用 User Model 的任何方法。 * 无论实际返回的方法如何,返回值都将始终为值。 在下面的例子中,它将返回 User Model 而不是布尔值。 * update 方法返回布尔值,但由于用了 tap ,所以它将返回 User Model。 */ $user = App\User::find(1); return tap($user)->update([ 'name' => 'SomeName' ]);
Copy after login

dump()

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);
Copy after login

str_slug()

Generate 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
Copy after login

optional()

Accepts a parameter, you can call the parameter's method or access the property. If the passed object 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;
Copy after login

For more Laravel related technical articles, please visit theLaravel Tutorialcolumn 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!

Related labels:
source:php.cn
Statement of this Website
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!