• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

Laravel 常用辅助函数

武飞扬头像
知其黑、受其白
帮助1

Laravel 常用辅助函数

辅助函数

Laravel 包含各种全局辅助函数。
laravel 中包含大量辅助函数,您可以使用它们来简化开发工作流程。

array_dot() 辅助函数允许你将多维数组转换为使用点符号的一维数组

$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 = [
    'user' => ['username' => 'something'],
    'app' => ['creator' => ['name' => 'someone'], 'created' => 'today']
];

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

// someone

如果 key 不存在,array_get() 函数还接受可选的第三个参数作为默认值。

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

// anonymous

public_path() 返回 Laravel 应用程序中公共目录的完全限定的绝对路径

还可以将路径传递到公共目录中的文件或目录以获取该资源的绝对路径。
它将简单地将 public_path() 添加到你的参数中。

$public_path = public_path();

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

Str::orderedUuid() 函数首先生成一个时间戳 uuid

这个uuid可以存储在索引数据库列中。

这些uuid是基于时间戳创建的,因此它们会保留你的内容索引。

在Laravel 5.6中使用它时,
会引发Ramsey\Uuid\Exception\UnsatisfiedDependencyException
要解决此问题,只需运行以下命令即可使用 moontoast/math 包:

composer require "moontoast/math"
use Illuminate\Support\Str;

return (string) Str::orderByUuid()

// A timestamp first uuid

str_plural 函数将字符串转换为复数形式,该功能只支持英文

echo str_plural('bank');

// banks

echo str_plural('developer');

// developers

route() 函数为指定的路由生成路由URL

$url = route('login');

如果路由接受参数,你可以简单地将它们作为第二个参数传递给一个数组。

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

如果你想产生一个相对的URL而不是一个绝对的URL,你可以传递 false 作为第三个参数。

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

tap() 函数接受两个参数:一个值和一个闭包

该值将被传递给闭包,然后该值将被返回。
闭包返回值无关紧要。

$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'
]);

optional() 函数接受一个参数,你可以调用参数的方法或访问属性

如果传递的对象为 null,则方法和属性将返回 null,而不是导致错误或抛出异常。

$user = User::find(1);

return optional($user)->name;

集合

一维数组去重 duplicates

duplicates 方法可以检索出集合中所有重复的值。返回的数组中会包含每个值在原数组中的键。

<?php
$collection = collect(['james', 'lisa', 'ryan', 'james', 'brad', 'lisa']);
$collection->duplicates(); // [3 => 'james', 5 => 'lisa']

遍历集合 each

在不使用 foreach 的情况下也可以遍历集合,就是使用 each 方法。像通常的 foreach 方法一样, each 方法也有两个参数,分别是 itemkey

<?php
$collection->each(function ($item, $key) {
    // Do stuff
});

集合 has 方法可以用来查看某个键是否存在于集合中

参数可以是字符串,也可以是数组。

如果传了数组作为参数,那么数组中所有的值都必须是集合中的键,结果才能返回 true,否则返回 false

<?php
$collection = collect([
  'title' => 'Harry Potter', 
  'author' => 'J.K. Rowling', 
  'price' => 25
  ]);
  
$collection->has('author'); // true
$collection->has(['title', 'price']); // true
$collection->has(['price', 'rating']); // false

集合 implode 方法可以连接集合中的值

这个方法的作用跟 PHP 中的 implode 方法非常像。

<?php

$collection = collect([
    ['title' => 'Gift card', 'price' => 50],
    ['title' => 'Chair', 'price' => 80],
]);
$collection->implode('title', ', ');
// Gift card, Chair

如果集合中的值只是字符串,或数字,你只需要传递连接符作为参数即可。

<?php
$collection = collect([1,2,'foo',3,'bar']);
$collection->implode('-');
// 1-2-foo-3-bar

集合 Push 与 Pull

可以使用 push 方法将项目附加到集合的末尾。
如果需要添加到集合的开头,则可以使用 prepend 方法。

<?php

$collection = collect([1, 2, 3]);
$collection->push(4);
$collection->all(); // [1, 2, 3, 4]

pull 方法将给定键从集合中移除并将返回其对应的值。

<?php

$collection = collect([
    'title' => 'Harry Potter',
    'author' => 'J.K. Rowling',
    'price' => 25
]);
$collection->pull('author'); // 'J.K. Rowling'
$collection->all(); // ['title' => 'Harry Potter', 'price' => 25]

集合 Shuffle 方法

shuffle 方法将会随机排序集合中的项目。

<?php

$collection = collect([1, 2, 3]);
$shuffled = $collection->shuffle();
$shuffled->all(); // [3, 1, 2] (random example)

集合 Max 方法

您可以使用 max 方法获取集合中的最大值。如果集合中包含数组,则可以传递参数以获取某个键的最大值。

<?php

$max = collect([
    ['title' => 'Gift card', 'price' => 50],
    ['title' => 'Chair', 'price' => 80]
])->max('price'); // 80
$max = collect([1, 2, 3])->max(); // 3

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgfechc
系列文章
更多 icon
同类精品
更多 icon
继续加载