辅助函数
简介
Laravel 包含各种各样的全局 PHP 「辅助」函数,框架本身也大量的使用了这些功能函数;如果你觉的方便,你可以在你的应用中任意使用这些函数。
可用方法
数组 & 对象
Arr::accessible Arr::add Arr::collapse Arr::crossJoin Arr::divide Arr::dot Arr::except Arr::exists Arr::first Arr::flatten Arr::forget Arr::get Arr::has Arr::hasAny Arr::isAssoc Arr::isList Arr::join Arr::keyBy Arr::last Arr::map Arr::only Arr::pluck Arr::prepend Arr::prependKeysWith Arr::pull Arr::query Arr::random Arr::set Arr::shuffle Arr::sort Arr::sortDesc Arr::sortRecursive Arr::toCssClasses Arr::undot Arr::where Arr::whereNotNull Arr::wrap data_fill data_get data_set head last
路径
app_path base_path config_path database_path lang_path mix public_path resource_path storage_path
字符串
__ class_basename e preg_replace_array Str::after Str::afterLast Str::ascii Str::before Str::beforeLast Str::between Str::betweenFirst Str::camel Str::contains Str::containsAll Str::endsWith Str::excerpt Str::finish Str::headline Str::inlineMarkdown Str::is Str::isAscii Str::isJson Str::isUlid Str::isUuid Str::kebab Str::lcfirst Str::length Str::limit Str::lower Str::markdown Str::mask Str::orderedUuid Str::padBoth Str::padLeft Str::padRight Str::password Str::plural Str::pluralStudly Str::random Str::remove Str::replace Str::replaceArray Str::replaceFirst Str::replaceLast Str::reverse Str::singular Str::slug Str::snake Str::squish Str::start Str::startsWith Str::studly Str::substr Str::substrCount Str::substrReplace Str::swap Str::title Str::toHtmlString Str::ucfirst Str::ucsplit Str::upper Str::ulid Str::uuid Str::wordCount Str::words str trans trans_choice
字符流处理
after afterLast append ascii basename before beforeLast between betweenFirst camel classBasename contains containsAll dirname endsWith excerpt exactly explode finish headline inlineMarkdown is isAscii isEmpty isNotEmpty isJson isUlid isUuid kebab lcfirst length limit lower ltrim markdown mask match matchAll isMatch newLine padBoth padLeft padRight pipe plural prepend remove replace replaceArray replaceFirst replaceLast replaceMatches rtrim scan singular slug snake split squish start startsWith studly substr substrReplace swap tap test title trim ucfirst ucsplit upper when whenContains whenContainsAll whenEmpty whenNotEmpty whenStartsWith whenEndsWith whenExactly whenNotExactly whenIs whenIsAscii whenIsUlid whenIsUuid whenTest wordCount words
URLs
action asset route secure_asset secure_url to_route url
杂项
abort abort_if abort_unless app auth back bcrypt blank broadcast cache class_uses_recursive collect config cookie csrf_field csrf_token decrypt dd dispatch dump encrypt env event fake filled info logger method_field now old optional policy redirect report report_if report_unless request rescue resolve response retry session tap throw_if throw_unless today trait_uses_recursive transform validator value view with
方法列表
数组 & 对象
Arr::accessible()
Arr::accessible
方法检查给定的值是否可被数组式访问:
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
$isAccessible = Arr::accessible(['a' => 1, 'b' => 2]);
// true
$isAccessible = Arr::accessible(new Collection);
// true
$isAccessible = Arr::accessible('abc');
// false
$isAccessible = Arr::accessible(new stdClass);
// false
Arr::add()
如果给定的键名在数组中不存在键值或该键值设置为 null
,那么 Arr::add
方法将会把给定的键值对添加到数组中:
use Illuminate\Support\Arr;
$array = Arr::add(['name' => 'Desk'], 'price', 100);
// ['name' => 'Desk', 'price' => 100]
$array = Arr::add(['name' => 'Desk', 'price' => null], '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::crossJoin()
Arr::crossJoin
方法交叉连接给定的数组,返回具有所有可能排列的笛卡尔乘积:
use Illuminate\Support\Arr;
$matrix = Arr::crossJoin([1, 2], ['a', 'b']);
/*
[
[1, 'a'],
[1, 'b'],
[2, 'a'],
[2, 'b'],
]
*/
$matrix = Arr::crossJoin([1, 2], ['a', 'b'], ['I', 'II']);
/*
[
[1, 'a', 'I'],
[1, 'a', 'II'],
[1, 'b', 'I'],
[1, 'b', 'II'],
[2, 'a', 'I'],
[2, 'a', 'II'],
[2, 'b', 'I'],
[2, 'b', 'II'],
]
*/
Arr::divide()
Arr::divide
方法返回一个二维数组,一个值包含原数组的键,另一个值包含原数组的 值:
use Illuminate\Support\Arr;
[$keys, $values] = Arr::divide(['name' => 'Desk']);
// $keys: ['name']
// $values: ['Desk']