辅助函数
简介
Laravel 包含各种各样的「全局」PHP 辅助函数,框架本身也大量的使用了这些功能函数;如果你觉的方便,你可以在你的应用中任意使用这些函数
可用方法
数组 & 对象
Arr::add
Arr::collapse
Arr::divide
Arr::dot
Arr::except
Arr::first
Arr::flatten
Arr::forget
Arr::get
Arr::has
Arr::last
Arr::only
Arr::pluck
Arr::prepend
Arr::pull
Arr::random
Arr::set
Arr::sort
Arr::sortRecursive
Arr::where
Arr::wrap
data_fill
data_get
data_set
head
last
路径
字符串
__
Str::camel
class_basename
e
Str::endsWith
Str::kebab
preg_replace_array
Str::snake
Str::startsWith
Str::after
Str::before
Str::contains
Str::finish
Str::is
Str::limit
Str::orderedUuid
Str::plural
Str::random
Str::replaceArray
Str::replaceFirst
Str::replaceLast
Str::singular
Str::slug
Str::start
Str::studly
Str::title
trans
trans_choice
Str::uuid
URLs
其他
abort
abort_if
abort_unless
app
auth
back
bcrypt
blank
broadcast
cache
class_uses_recursive
collect
config
cookie
csrf_field
csrf_token
dd
decrypt
dispatch
dispatch_now
dump
encrypt
env
event
factory
filled
info
logger
method_field
now
old
optional
policy
redirect
report
request
rescue
resolve
response
retry
session
tap
today
throw_if
throw_unless
trait_uses_recursive
transform
validator
value
view
with
方法列表
数组 & 对象
Arr::add()
如果给定的键不存在数组中,那么 Arr::add 函数将会把给定的键/值对添加到数组中:
use Illuminate\Support\Arr; $array = Arr::add(['name' => 'Desk'], 'price', 100); // ['name' => 'Desk', 'price' => 100]
Arr::collapse()
Arr::collapse 函数将多个数组合并为一个数组:
use Illuminate\Support\Arr; $array = Arr::collapse([[1, 2, 3], [4, 5, 6], [7, 8, 9]]); // [1, 2, 3, 4, 5, 6, 7, 8, 9]
Arr::divide()
Arr::divide 函数返回一个二维数组,一个值包含原始数组的键,另一个值包含原始数组的值:
use Illuminate\Support\Arr; [$keys, $values] = Arr::divide(['name' => 'Desk']); // $keys: ['name'] 多维数组中的第0个值 // $values: ['Desk'] 多维数组中的第1个值
Arr::dot()
Arr::dot 函数将多维数组中所有的键平铺到一维数组中,新数组使用「.」符号表示层级包含关系:
use Illuminate\Support\Arr; $array = ['products' => ['desk' => ['price' => 100]]]; $flattened = Arr::dot($array); // ['products.desk.price' => 100]
Arr::except()
Arr::except 函数从数组中删除给定的键/值对:
use Illuminate\Support\Arr; $array = ['name' => 'Desk', 'price' => 100]; $filtered = Arr::except($array, ['price']); // ['name' => 'Desk']
Arr::first()
Arr::first 函数返回数组中通过指定测试的第一个元素:
use Illuminate\Support\Arr;
$array = [100, 200, 300];
$first = Arr::first($array, function ($value, $key) {
return $value >= 150;
});
// 200将默认值作为第三个参数传递给该方法, 如果数组中没有值通过测试,则返回默认值:
use Illuminate\Support\Arr; $first = Arr::first($array, $callback, $default);
Arr::flatten()
Arr::flatten 函数将多维数组中数组的值取出平铺为一维数组:
use Illuminate\Support\Arr; $array = ['name' => 'Joe', 'languages' => ['PHP', 'Ruby']]; $flattened = Arr::flatten($array); // ['Joe', 'PHP', 'Ruby']
Arr::forget()
Arr::forget 函数使用「.」符号从深度嵌套的数组中删除给定的键/值对:
use Illuminate\Support\Arr; $array = ['products' => ['desk' => ['price' => 100]]]; Arr::forget($array, 'products.desk'); // ['products' => []]
Arr::get()
Arr::get 函数使用「.」符号从深度嵌套的数组中根据指定键检索值:
use Illuminate\Support\Arr; $array = ['products' => ['desk' => ['price' => 100]]]; $price = Arr::get($array, 'products.desk.price'); // 100
Arr::get 函数也接受一个默认值,如果没有找到特定的键,将返回默认值:
use Illuminate\Support\Arr; $discount = Arr::get($array, 'products.desk.discount', 0); // 0
Arr::has()
Arr::has 函数使用「.」符号查找数组中是否存在指定的一个或多个键:
use Illuminate\Support\Arr; $array = ['product' => ['name' => 'Desk', 'price' => 100]]; $contains = Arr::has($array, 'product.name'); // true $contains = Arr::has($array, ['product.price', 'product.discount']); // false
Arr::last()
Arr::last 函数返回数组中通过指定测试的最后一个元素:
use Illuminate\Support\Arr;
$array = [100, 200, 300, 110];
$last = Arr::last($array, function ($value, $key) {
return $value >= 150;
});
// 300将默认值作为第三个参数传递给该方法,如果没有值通过指定测试,则返回该默认值:
use Illuminate\Support\Arr; $last = Arr::last($array, $callback, $default);
Arr::only()
Arr::only 函数只返回给定数组中指定的键/值对:
use Illuminate\Support\Arr; $array = ['name' => 'Desk', 'price' => 100, 'orders' => 10]; $slice = Arr::only($array, ['name', 'price']); // ['name' => 'Desk', 'price' => 100]
Arr::pluck()
Arr::pluck 函数从数组中检索给定键的所有值:
use Illuminate\Support\Arr;$array = [ ['developer' => ['id' => 1, 'name' => 'Taylor']], ['developer' => ['id' => 2, 'name' => 'Abigail']], ]; $names = Arr::pluck($array, 'developer.name'); // ['Taylor', 'Abigail']
你也可以指定获取的结果的键:
use Illuminate\Support\Arr; $names = Arr::pluck($array, 'developer.name', 'developer.id'); // [1 => 'Taylor', 2 => 'Abigail']
Arr::prepend()
Arr::prepend 函数将一个值插入到数组的开始位置:
use Illuminate\Support\Arr; $array = ['one', 'two', 'three', 'four']; $array = Arr::prepend($array, 'zero'); // ['zero', 'one', 'two', 'three', 'four']
如果需要,你可以指定你插入值的键:
use Illuminate\Support\Arr; $array = ['price' => 100]; $array = Arr::prepend($array, 'Desk', 'name'); // ['name' => 'Desk', 'price' => 100]
Arr::pull()
Arr::pull 函数从数组中返回指定键的值并删除此键/值对:
use Illuminate\Support\Arr; $array = ['name' => 'Desk', 'price' => 100]; $name = Arr::pull($array, 'name'); // $name: Desk // $array: ['price' => 100]
默认值可以作为第三个参数传递给该方法,如果键不存在,则返回该值:
use Illuminate\Support\Arr; $value = Arr::pull($array, $key, $default);
Arr::random()
Arr::random 函数从数组中随机返回一个值:
use Illuminate\Support\Arr; $array = [1, 2, 3, 4, 5]; $random = Arr::random($array); // 4 - (随机检索)
你也可以将返回值的数量作为可选的第二个参数传递给该方法,请注意,提供这个参数会返回一个数组,即便是你只需要一项:
use Illuminate\Support\Arr; $items = Arr::random($array, 2); // [2, 5] - (随机检索)
Arr::set()
Arr::set 函数使用「.」符号在多维数组中设置指定键的值:
use Illuminate\Support\Arr; $array = ['products' => ['desk' => ['price' => 100]]]; Arr::set($array, 'products.desk.price', 200); // ['products' => ['desk' => ['price' => 200]]]
Arr::sort()
Arr::sort 函数根据数组的值对数组进行排序:
use Illuminate\Support\Arr;$array = ['Desk', 'Table', 'Chair']; $sorted = Arr::sort($array); // ['Chair', 'Desk', 'Table']
你也可以根据给定闭包返回的结果对数组进行排序:
use Illuminate\Support\Arr;$array = [
['name' => 'Desk'],
['name' => 'Table'],
['name' => 'Chair'],
];
$sorted = array_values(Arr::sort($array, function ($value) {
return $value['name'];
})
);
/*
[
['name' => 'Chair'],
['name' => 'Desk'],
['name' => 'Table'],
]
*/Arr::sortRecursive()
Arr::sortRecursive 函数使用 sort 函数对数组进行递归排序:
use Illuminate\Support\Arr;$array = [ ['Roman', 'Taylor', 'Li'], ['PHP', 'Ruby', 'JavaScript'], ['one' => 1, 'two' => 2, 'three' => 3], ]; $sorted = Arr::sortRecursive($array); /* [ ['JavaScript', 'PHP', 'Ruby'], ['one' => 1, 'three' => 3, 'two' => 2], ['Li', 'Roman', 'Taylor'], ] */
Arr::where()
Arr::where 函数使用给定闭包返回的结果过滤数组:
use Illuminate\Support\Arr;
$array = [100, '200', 300, '400', 500];
$filtered = Arr::where($array, function ($value, $key) {
return is_string($value);
});
// [1 => '200', 3 => '400']Arr::wrap()
Arr::wrap 函数将给定的值变为一个数组,如果给定的值已经是数组,则不改变:
use Illuminate\Support\Arr; $string = 'Laravel'; $array = Arr::wrap($string); // ['Laravel']
如果给定的值是空,则返回一个空数组:
use Illuminate\Support\Arr; $nothing = null; $array = Arr::wrap($nothing); // []
data_fill()
data_fill 函数使用「.」符号在多维数组或对象内设置缺省值:
$data = ['products' => ['desk' => ['price' => 100]]]; data_fill($data, 'products.desk.price', 200); // ['products' => ['desk' => ['price' => 100]]] data_fill($data, 'products.desk.discount', 10); // ['products' => ['desk' => ['price' => 100, 'discount' => 10]]]
这个函数还接受星号「*」作为通配符,相应的填充目标:
$data = [ 'products' => [ ['name' => 'Desk 1', 'price' => 100], ['name' => 'Desk 2'], ], ]; data_fill($data, 'products.*.price', 200); /* [ 'products' => [ ['name' => 'Desk 1', 'price' => 100], ['name' => 'Desk 2', 'price' => 200], ], ] */
data_get()
data_get 函数使用「.」符号从多维数组或对象中检索值
$data = ['products' => ['desk' => ['price' => 100]]]; $price = data_get($data, 'products.desk.price'); // 100
data_get 函数也可以接收一个默认值,如果找不到指定的键,则返回默认值:
$discount = data_get($data, 'products.desk.discount', 0); // 0
这个函数还接受「*」作为通配符,它可以匹配数组或对象的任何键:
$data = [ 'product-one' => ['name' => 'Desk 1', 'price' => 100], 'product-two' => ['name' => 'Desk 2', 'price' => 150], ]; data_get($data, '*.name'); // ['Desk 1', 'Desk 2'];
data_set()
data_set 函数使用「.」符号在多维数组或对象中设置一个值:
$data = ['products' => ['desk' => ['price' => 100]]]; data_set($data, 'products.desk.price', 200); // ['products' => ['desk' => ['price' => 200]]]
该函数也可以接收「*」通配符,相应的在指定键上设置值:
$data = [ 'products' => [ ['name' => 'Desk 1', 'price' => 100], ['name' => 'Desk 2', 'price' => 150], ], ]; data_set($data, 'products.*.price', 200); /* [ 'products' => [ ['name' => 'Desk 1', 'price' => 200], ['name' => 'Desk 2', 'price' => 200], ], ] */
默认情况下,所有现有值都会被覆盖, 如果你只希望设置不存在的值,你可以选择 false 作为第四个参数传递给该方法:
$data = ['products' => ['desk' => ['price' => 100]]]; data_set($data, 'products.desk.price', 200, false); // ['products' => ['desk' => ['price' => 100]]]
head()
head 函数返回给定数组中的第一个元素:
$array = [100, 200, 300]; $first = head($array); // 100
last()
last 函数返回给定数组中的最后一个元素:
$array = [100, 200, 300]; $last = last($array); // 300
路径
app_path()
app_path 函数返回 app 目录的完整路径.你也可以使用

