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
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
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
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');
(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
Convert a string to plural form. This feature only supports English.
echo str_plural('bank'); // banks echo str_plural('developer'); // developers
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);
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' ]);
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);
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
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;
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!