# Laravel 的輔助函數列表
- [簡介](#introduction)
- [可用方法](#available-methods)
<a name="introduction"></a>
## 簡介
Laravel 包含各種各樣的全局「輔助」PHP 函數,這些方法中的很多方法都在 Laravel 框架中使用;如果你覺得方便,你可以在你的應用中自由的使用它們。
<a name="available-methods"></a>
## 可用方法
<style>
.collection-method-list > p {
column-count: 3; -moz-column-count: 3; -webkit-column-count: 3;
column-gap: 2em; -moz-column-gap: 2em; -webkit-column-gap: 2em;
}
.collection-method-list a {
display: block;
}
</style>
### 數組
<div class="collection-method-list" markdown="1">
[array_add](#method-array-add)
[array_collapse](#method-array-collapse)
[array_divide](#method-array-divide)
[array_dot](#method-array-dot)
[array_except](#method-array-except)
[array_first](#method-array-first)
[array_flatten](#method-array-flatten)
[array_forget](#method-array-forget)
[array_get](#method-array-get)
[array_has](#method-array-has)
[array_last](#method-array-last)
[array_only](#method-array-only)
[array_pluck](#method-array-pluck)
[array_prepend](#method-array-prepend)
[array_pull](#method-array-pull)
[array_set](#method-array-set)
[array_sort](#method-array-sort)
[array_sort_recursive](#method-array-sort-recursive)
[array_where](#method-array-where)
[array_wrap](#method-array-wrap)
[head](#method-head)
[last](#method-last)
</div>
### 路徑
<div class="collection-method-list" markdown="1">
[app_path](#method-app-path)
[base_path](#method-base-path)
[config_path](#method-config-path)
[database_path](#method-database-path)
[mix](#method-mix)
[public_path](#method-public-path)
[resource_path](#method-resource-path)
[storage_path](#method-storage-path)
</div>
### 字符串
<div class="collection-method-list" markdown="1">
[camel_case](#method-camel-case)
[class_basename](#method-class-basename)
[e](#method-e)
[ends_with](#method-ends-with)
[kebab_case](#method-kebab-case)
[snake_case](#method-snake-case)
[str_limit](#method-str-limit)
[starts_with](#method-starts-with)
[str_after](#method-str-after)
[str_before](#method-str-before)
[str_contains](#method-str-contains)
[str_finish](#method-str-finish)
[str_is](#method-str-is)
[str_plural](#method-str-plural)
[str_random](#method-str-random)
[str_singular](#method-str-singular)
[str_slug](#method-str-slug)
[studly_case](#method-studly-case)
[title_case](#method-title-case)
[trans](#method-trans)
[trans_choice](#method-trans-choice)
</div>
### URLs
<div class="collection-method-list" markdown="1">
[action](#method-action)
[asset](#method-asset)
[secure_asset](#method-secure-asset)
[route](#method-route)
[secure_url](#method-secure-url)
[url](#method-url)
</div>
### 其他
<div class="collection-method-list" markdown="1">
[abort](#method-abort)
[abort_if](#method-abort-if)
[abort_unless](#method-abort-unless)
[auth](#method-auth)
[back](#method-back)
[bcrypt](#method-bcrypt)
[cache](#method-cache)
[collect](#method-collect)
[config](#method-config)
[csrf_field](#method-csrf-field)
[csrf_token](#method-csrf-token)
[dd](#method-dd)
[dispatch](#method-dispatch)
[env](#method-env)
[event](#method-event)
[factory](#method-factory)
[info](#method-info)
[logger](#method-logger)
[method_field](#method-method-field)
[old](#method-old)
[redirect](#method-redirect)
[report](#method-report)
[request](#method-request)
[response](#method-response)
[retry](#method-retry)
[session](#method-session)
[tap](#method-tap)
[value](#method-value)
[view](#method-view)
</div>
<a name="method-listing"></a>
## 方法列表
<style>
#collection-method code {
font-size: 14px;
}
#collection-method:not(.first-collection-method) {
margin-top: 50px;
}
</style>
<a name="arrays"></a>
## 數組
<a name="method-array-add"></a>
#### `array_add()` {#collection-method .first-collection-method}
如果給定的健不在數組中,那么 `array_add` 函數將會把給定健值對添加到數組中:
$array = array_add(['name' => 'Desk'], 'price', 100);
// ['name' => 'Desk', 'price' => 100]
<a name="method-array-collapse"></a>
#### `array_collapse()` {#collection-method}
`array_collapse` 函數把數組中的每一個數組合并成單個數組:
$array = array_collapse([[1, 2, 3], [4, 5, 6], [7, 8, 9]]);
// [1, 2, 3, 4, 5, 6, 7, 8, 9]
<a name="method-array-divide"></a>
#### `array_divide()` {#collection-method}
`array_divide` 函數返回兩個數組,一個包含原始數組的健,另一個包含原始數組的值:
list($keys, $values) = array_divide(['name' => 'Desk']);
// $keys: ['name']
// $values: ['Desk']
<a name="method-array-dot"></a>
#### `array_dot()` {#collection-method}
`array_dot` 函數將多維數組平坦化為使用「點」符號表示深度的一維數組:
$array = array_dot(['foo' => ['bar' => 'baz']]);
// ['foo.bar' => 'baz'];
<a name="method-array-except"></a>
#### `array_except()` {#collection-method}
`array_except` 函數從數組中刪除指定的健值對:
$array = ['name' => 'Desk', 'price' => 100];
$array = array_except($array, ['price']);
// ['name' => 'Desk']
<a name="method-array-first"></a>
#### `array_first()` {#collection-method}
`array_first` 函數返回數組中第一個通過指定測試的元素:
$array = [100, 200, 300];
$value = array_first($array, function ($value, $key) {
return $value >= 150;
});
// 200
也可以將默認值作為第三個參數傳遞給方法。如果沒有值通過測試,則返回默認值:
$value = array_first($array, $callback, $default);
<a name="method-array-flatten"></a>
#### `array_flatten()` {#collection-method}
`array_flatten` 函數將多維數組平坦化為一維數組。
$array = ['name' => 'Joe', 'languages' => ['PHP', 'Ruby']];
$array = array_flatten($array);
// ['Joe', 'PHP', 'Ruby'];
<a name="method-array-forget"></a>
#### `array_forget()` {#collection-method}
`array_forget` 函數使用「點」表示法從一個深度嵌套的數組中刪除給定的健值對:
$array = ['products' => ['desk' => ['price' => 100]]];
array_forget($array, 'products.desk');
// ['products' => []]
<a name="method-array-get"></a>
#### `array_get()` {#collection-method}
`array_get` 函數使用「點」符號從深度嵌套的數組中檢索一個值:
$array = ['products' => ['desk' => ['price' => 100]]];
$value = array_get($array, 'products.desk');
// ['price' => 100]
`array_get` 函數也接受一個默認值,如果沒有找到指定的健,則返回默認值:
$value = array_get($array, 'names.john', 'default');
<a name="method-array-has"></a>
#### `array_has()` {#collection-method}
`array_has` 使用「點」表示法檢查數組中是否存在指定的項目:
$array = ['product' => ['name' => 'desk', 'price' => 100]];
$hasItem = array_has($array, 'product.name');
// true
$hasItems = array_has($array, ['product.price', 'product.discount']);
// false
<a name="method-array-last"></a>
#### `array_last()` {#collection-method}
`array_last` 函數返回數組中最后一個通過指定測試的元素:
$array = [100, 200, 300, 110];
$value = array_last($array, function ($value, $key) {
return $value >= 150;
});
// 300
<a name="method-array-only"></a>
#### `array_only()` {#collection-method}
`array_only` 函數只返回給定的數組中指定的健值對:
$array = ['name' => 'Desk', 'price' => 100, 'orders' => 10];
$array = array_only($array, ['name', 'price']);
// ['name' => 'Desk', 'price' => 100]
<a name="method-array-pluck"></a>
#### `array_pluck()` {#collection-method}
`array_pluck` 函數將從數組中提取出一列給定的健值對:
$array = [
['developer' => ['id' => 1, 'name' => 'Taylor']],
['developer' => ['id' => 2, 'name' => 'Abigail']],
];
$array = array_pluck($array, 'developer.name');
// ['Taylor', 'Abigail'];
你也可以指定生成的列表的想要的健是什么:
$array = array_pluck($array, 'developer.name', 'developer.id');
// [1 => 'Taylor', 2 => 'Abigail'];
<a name="method-array-prepend"></a>
#### `array_prepend()` {#collection-method}
`array_prepend` 函數將一個項目推到數組的開始位置:
$array = ['one', 'two', 'three', 'four'];
$array = array_prepend($array, 'zero');
// $array: ['zero', 'one', 'two', 'three', 'four']
<a name="method-array-pull"></a>
#### `array_pull()` {#collection-method}
`array_pull` 函數從數組移除指定鍵值對并返回該鍵值對:
$array = ['name' => 'Desk', 'price' => 100];
$name = array_pull($array, 'name');
// $name: Desk
// $array: ['price' => 100]
<a name="method-array-set"></a>
#### `array_set()` {#collection-method}
`array_set` 函數使用「點」表示法在深度嵌套的數組中設置一個值:
$array = ['products' => ['desk' => ['price' => 100]]];
array_set($array, 'products.desk.price', 200);
// ['products' => ['desk' => ['price' => 200]]]
<a name="method-array-sort"></a>
#### `array_sort()` {#collection-method}
`array_sort` 函數根據給定的閉包的結果對數組進行排序:
$array = [
['name' => 'Desk'],
['name' => 'Chair'],
];
$array = array_values(array_sort($array, function ($value) {
return $value['name'];
}));
/*
[
['name' => 'Chair'],
['name' => 'Desk'],
]
*/
<a name="method-array-sort-recursive"></a>
#### `array_sort_recursive()` {#collection-method}
`array_sort_recursive` 使用 `sort` 函數遞歸排序數組:
$array = [
[
'Roman',
'Taylor',
'Li',
],
[
'PHP',
'Ruby',
'JavaScript',
],
];
$array = array_sort_recursive($array);
/*
[
[
'Li',
'Roman',
'Taylor',
],
[
'JavaScript',
'PHP',
'Ruby',
]
];
*/
<a name="method-array-where"></a>
#### `array_where()` {#collection-method}
`array_where` 函數使用給定的閉包過濾數組:
$array = [100, '200', 300, '400', 500];
$array = array_where($array, function ($value, $key) {
return is_string($value);
});
// [1 => 200, 3 => 400]
<a name="method-array-wrap"></a>
#### `array_wrap()` {#collection-method}
`array_wrap` 函數將給定的值包裝成一個數組。如果給定的值已經是一個數組,則不會被改變:
$string = 'Laravel';
$array = array_wrap($string);
// [0 => 'Laravel']
<a name="method-head"></a>
#### `head()` {#collection-method}
`head` 函數返回給定數組中的第一個元素:
$array = [100, 200, 300];
$first = head($array);
// 100
<a name="method-last"></a>
#### `last()` {#collection-method}
`last` 函數返回給定數組中的最后一個元素:
$array = [100, 200, 300];
$last = last($array);
// 300
<a name="paths"></a>
## 路徑
<a name="method-app-path"></a>
#### `app_path()` {#collection-method}
`app_path` 返回 `app` 目錄的完整路徑。你還可以使用 `app_path` 函數來生成相對于 `app` 目錄的文件完整路徑:
$path = app_path();
$path = app_path('Http/Controllers/Controller.php');
<a name="method-base-path"></a>
#### `base_path()` {#collection-method}
`base_path` 函數返回項目根目錄的完整路徑。你還可以使用 `base_path` 函數生成指定文件相對于項目根目錄的完整路徑:
$path = base_path();
$path = base_path('vendor/bin');
<a name="method-config-path"></a>
#### `config_path()` {#collection-method}
`config_path` 函數返回應用程序配置目錄的完整路徑:
$path = config_path();
<a name="method-database-path"></a>
#### `database_path()` {#collection-method}
`database_path` 函數返回應用程序數據庫目錄的完整路徑:
$path = database_path();
<a name="method-mix"></a>
#### `mix()` {#collection-method}
`mix` 函數獲取 [版本化 Mix 文件](/docs/{{version}}/mix) 文件的路徑:
mix($file);
<a name="method-public-path"></a>
#### `public_path()` {#collection-method}
`public_path` 函數返回 `public` 目錄的完整路徑:
$path = public_path();
<a name="method-resource-path"></a>
#### `resource_path()` {#collection-method}
`resource_path` 函數返回 `resources` 目錄的完整路徑。你還可以使用 `resource_path` 函數來生成相對于資源目錄的指定文件的完整路徑:
$path = resource_path();
$path = resource_path('assets/sass/app.scss');
<a name="method-storage-path"></a>
#### `storage_path()` {#collection-method}
`storage_path` 函數返回 `storage` 目錄的完整路徑。你還可以使用 `storage_path` 來生成相對于儲存目錄的指定文件的完整路徑:
$path = storage_path();
$path = storage_path('app/file.txt');
<a name="strings"></a>
## 字符串
<a name="method-camel-case"></a>
#### `camel_case()` {#collection-method}
`camel_case` 函數將給定的值符傳轉換為 `駝峰命名`:
$camel = camel_case('foo_bar');
// fooBar
<a name="method-class-basename"></a>
#### `class_basename()` {#collection-method}
`class_basename` 返回給定類刪除命名空間的類名:
$class = class_basename('Foo\Bar\Baz');
// Baz
<a name="method-e"></a>
#### `e()` {#collection-method}
`e` 函數使用 PHP 函數 `htmlspecialchars` 并且 `double_encode` 選項設置為 `false`:
echo e('<html>foo</html>');
// <html>foo</html>
<a name="method-ends-with"></a>
#### `ends_with()` {#collection-method}
`ends_with` 函數判斷給定的字符串結尾是否是指定的內容:
$value = ends_with('This is my name', 'name');
// true
<a name="method-kebab-case"></a>
#### `kebab_case()` {#collection-method}
`lebab_case` 函數將給定的字符串轉換為 `短橫線隔開式`:
$value = kebab_case('fooBar');
// foo-bar
<a name="method-snake-case"></a>
#### `snake_case()` {#collection-method}
`snake_case` 函數將給定的字符串轉換為 `蛇形命名`:
$snake = snake_case('fooBar');
// foo_bar
<a name="method-str-limit"></a>
#### `str_limit()` {#collection-method}
`str_limit` 函數限制字符串的字符數。該函數第一個參數接受一個字符串,第二個參數作為允許的最大字符數。
$value = str_limit('The PHP framework for web artisans.', 7);
// The PHP...
<a name="method-starts-with"></a>
#### `starts_with()` {#collection-method}
`starts_with` 函數判斷給定的字符串的開頭是否是指定值:
$value = starts_with('This is my name', 'This');
// true
<a name="method-str-after"></a>
#### `str_after()` {#collection-method}
`str_after` 函數返回字符串中指定值之后的所有內容:
$value = str_after('This is: a test', 'This is:');
// ' a test'
<a name="method-str-before"></a>
#### `str_before()` {#collection-method}
`str_before` 函數返回字符串指定值之前的所有內容:
$value = str_before('Test :it before', ':it before');
// 'Test '
<a name="method-str-contains"></a>
#### `str_contains()` {#collection-method}
`str_contains` 函數判斷字符串是否包含指定的值:
$value = str_contains('This is my name', 'my');
// true
你還可以傳遞一個值的數組,來判斷字符串是否包任意指定內容:
$value = str_contains('This is my name', ['my', 'foo']);
// true
<a name="method-str-finish"></a>
#### `str_finish()` {#collection-method}
`str_finish` 函數添加一個如果沒有以指定值結尾的字符串后面:
$string = str_finish('this/string', '/');
$string2 = str_finish('this/string/', '/');
// this/string/
<a name="method-str-is"></a>
#### `str_is()` {#collection-method}
`str_is` 函數判斷指定的字符串是否匹配指定的格式。星號可以作為通配符使用:
$value = str_is('foo*', 'foobar');
// true
$value = str_is('baz*', 'foobar');
// false
<a name="method-str-plural"></a>
#### `str_plural()` {#collection-method}
`str_plural` 函數將字符串轉換為復數形式。這個函數目前僅支持英文:
$plural = str_plural('car');
// cars
$plural = str_plural('child');
// children
你可以給函數的第二個參數傳遞一個整數,來檢索字符串的單數形式或者復數形式:
$plural = str_plural('child', 2);
// children
$plural = str_plural('child', 1);
// child
<a name="method-str-random"></a>
#### `str_random()` {#collection-method}
`str_random` 函數生成一個指定長度的隨機字符串。這個函數數用 PHP 的 `random_bytes` 函數:
$string = str_random(40);
<a name="method-str-singular"></a>
#### `str_singular()` {#collection-method}
`str_singular` 函數將字符串轉換為單數形式。這個函數目前僅支持英文:
$singular = str_singular('cars');
// car
<a name="method-str-slug"></a>
#### `str_slug()` {#collection-method}
`str_slug` 函數根據給定的字符串生成一個友好的「slug」URL:
$title = str_slug('Laravel 5 Framework', '-');
// laravel-5-framework
<a name="method-studly-case"></a>
#### `studly_case()` {#collection-method}
`studly_case` 函數將給定的字符串轉換為 `首字母大寫`:
$value = studly_case('foo_bar');
// FooBar
<a name="method-title-case"></a>
#### `title_case()` {#collection-method}
`title_case` 函數將給定的字符串轉換為 `每個單詞首字母大寫`;
$title = title_case('a nice title uses the correct case');
// A Nice Title Uses The Correct Case
<a name="method-trans"></a>
#### `trans()` {#collection-method}
`trans` 函數使用你的 [本地化文件](/docs/{{version}}/localization) 來翻譯給定的語句:
echo trans('validation.required'):
<a name="method-trans-choice"></a>
#### `trans_choice()` {#collection-method}
`trans_choice` 函數根據給定數量來決定翻譯指定語句是復數形式還是單數形式:
$value = trans_choice('foo.bar', $count);
<a name="urls"></a>
## URLs
<a name="method-action"></a>
#### `action()` {#collection-method}
`action` 函數為指定的控制器動作生成一個 URL。你不需要傳遞完整的控制器命名空間。只需要傳遞相對于 `App\Http\Controllers` 的命名空間:
$url = action('HomeController@getIndex');
如果該方法接受路由參數,你可以使用第二個參數傳遞:
$url = action('UserController@profile', ['id' => 1]);
<a name="method-asset"></a>
#### `asset()` {#collection-method}
使用當前請求的協議( HTTP 或 HTTPS )為資源文件生成一個 URL:
$url = asset('img/photo.jpg');
<a name="method-secure-asset"></a>
#### `secure_asset()` {#collection-method}
使用 HTTPS 協議生成資源文件的 URL:
echo secure_asset('foo/bar.zip', $title, $attributes = []);
<a name="method-route"></a>
#### `route()` {#collection-method}
`route` 函數為給定的命名路由生成一個 URL:
$url = route('routeName');
如果路由接受參數,則可以使用第二個參數傳遞給方法:
$url = route('routeName', ['id' => 1]);
默認情況下,`route` 函數生成的是絕對 URL。如果你想生成一個相對 URL,你可以第三個值傳遞 `false`:
$url = route('routeName', ['id' => 1], false);
<a name="method-secure-url"></a>
#### `secure_url()` {#collection-method}
`secure_url` 函數為給定的路徑生成一個完整的 HTTPS URL 路徑:
echo secure_url('user/profile');
echo secure_url('user/profile', [1]);
<a name="method-url"></a>
#### `url()` {#collection-method}
`url` 函數生成給定的路徑的完整 URL:
echo url('user/profile');
echo url('user/profile', [1]);
如果沒有提供路徑,則返回 `Illuminate\Routing\UrlGenerator` 實例:
echo url()->current();
echo url()->full();
echo url()->previous();
<a name="miscellaneous"></a>
## 其他
<a name="method-abort"></a>
#### `abort()` {#collection-method}
`abort` 函數將會跑出一個 HTTP 異常并且由異常處理程序處理:
abort(401);
你還可以提供異常的響應文本:
abort(401, 'Unauthorized.');
<a name="method-abort-if"></a>
#### `abort_if()` {#collection-method}
如果給定的布爾值為 `true` 則 `abort_if` 函數將拋出一個 HTTP 異常:
abort_if(! Auth::user()->isAdmin(), 403);
<a name="method-abort-unless"></a>
#### `abort_unless()` {#collection-method}
如果給定的布爾值為 `false` 則 `abort_unless` 函數將拋出一個 HTTP 異常:
abort_unless(Auth::user()->isAdmin(), 403);
<a name="method-auth"></a>
#### `auth()` {#collection-method}
為例方便起見 `auth` 函數返回一個認證實例。你可以使用它來替代 `Auth` facade:
$user = auth()->user();
<a name="method-back"></a>
#### `back()` {#collection-method}
`back()` 函數會生成用戶之前位置的一個重定向響應:
return back();
<a name="method-bcrypt"></a>
#### `bcrypt()` {#collection-method}
`bcrypt` 使用 Bcrypt 對給定的值進行散列。你可以使用它替代 `Hash` facade:
$password = bcrypt('my-secret-password');
<a name="method-cache"></a>
#### `cache()` {#collection-method}
`cache` 函數可以用來從緩存中獲取值。如果緩存中不存在給定的健,則返回默認值:
$value = cache('key');
$value = cache('key', 'default');
你可以通過健值對的數組來添加項目到緩沖中。你還應該傳遞一個以分鐘為單位緩存過期時間:
cache(['key' => 'value'], 5);
cache(['key' => 'value'], Carbon::now()->addSeconds(10));
<a name="method-collect"></a>
#### `collect()` {#collection-method}
`collect` 函數根據給定的數組創建一個 [集合](/docs/{{version}}/collections) 實例:
$collection = collect(['taylor', 'abigail']);
<a name="method-config"></a>
#### `config()` {#collection-method}
`config` 函數用來獲取配置信息的值,可以使用「點」語法訪問配置值,其中要包含文件名和選項名。可以指定一個默認值,如果選項不存在則返回默認值:
$value = config('app.timezone');
$value = config('app.timezone', $default);
`config` 輔助函數也可以通過傳遞一個健值對數組在運行的時候配置信息:
config(['app.debug' => true]);
<a name="method-csrf-field"></a>
#### `csrf_field()` {#collection-method}
`csrf_field` 函數生成包含 CSRF 令牌值的 HTML `hidden` 表單字段。例如,使用 [Blade 語法](/docs/{{version}}/blade):
{{ csrf_field() }}
<a name="method-csrf-token"></a>
#### `csrf_token()` {#collection-method}
`csrf_token` 函數獲取當前 CSRF 令牌的值:
$token = csrf_token();
<a name="method-dd"></a>
#### `dd()` {#collection-method}
`dd` 函數輸出給定的值并結束腳本運行:
dd($value);
dd($value1, $value2, $value3, ...);
如果你不想終止腳本運行,請改用 `dump` 函數:
dump($value);
<a name="method-dispatch"></a>
#### `dispatch()` {#collection-method}
`dispatch` 函數將一個新的任務推送到 Laravel [任務列隊](/docs/{{version}}/queues)
dispatch(new App\Jobs\SendEmails);
<a name="method-env"></a>
#### `env()` {#collection-method}
`env` 函數獲取環境變量的值或者返回默認值:
$env = env('APP_ENV');
// 如果環境變量不存在則返回默認值...
$env = env('APP_ENV', 'production');
<a name="method-event"></a>
#### `event()` {#collection-method}
`event` 函數將給定的 [事件](/docs/{{version}}/events) 派發到所屬偵聽器:
event(new UserRegistered($user));
<a name="method-factory"></a>
#### `factory()` {#collection-method}
`factory` 函數根據給定的類、名稱和數量創建一個模型工廠構建器。可以在 [測試](/docs/{{version}}/database-testing#writing-factories) or [數據填充](/docs/{{version}}/seeding#using-model-factories) 中使用:
$user = factory(App\User::class)->make();
<a name="method-info"></a>
#### `info()` {#collection-method}
`info` 函數將信息寫入日志:
info('Some helpful information!');
上下文數據的數組也可以傳遞給函數:
info('User login attempt failed.', ['id' => $user->id]);
<a name="method-logger"></a>
#### `logger()` {#collection-method}
`logger` 函數可以將一個 `debug` 級別的消息寫入到乳汁中:
logger('Debug message');
上下文數據的數組也可以傳遞給函數:
logger('User has logged in.', ['id' => $user->id]);
如果沒有傳值給函數則返回 [日志](/docs/{{version}}/errors#logging) 的實例:
logger()->error('You are not allowed here.');
<a name="method-method-field"></a>
#### `method_field()` {#collection-method}
`method_field` 函數生成一個模擬 HTTP 動作的 HTML `hidden` 表單字段。例如,使用 [Blade 語法](/docs/{{version}}/blade):
<form method="POST">
{{ method_field('DELETE') }}
</form>
<a name="method-old"></a>
#### `old()` {#collection-method}
`old` 函數 [獲取](/docs/{{version}}/requests#retrieving-input) 一個舊的 session 閃存輸入值:
$value = old('value');
$value = old('value', 'default');
<a name="method-redirect"></a>
#### `redirect()` {#collection-method}
`redirect` 函數返回一個重定向 HTTP 響應,如果沒有沒有傳入參數,則返回重定向實例:
return redirect('/home');
return redirect()->route('route.name');
<a name="method-report"></a>
#### `report()` {#collection-method}
`report` 函數將使用異常處理程序的 `report` 方法拋出異常:
report($e);
<a name="method-request"></a>
#### `request()` {#collection-method}
`request` 函數返回當前 [請求](/docs/{{version}}/requests) 實例或者獲取輸入項:
$request = request();
$value = request('key', $default = null)
<a name="method-response"></a>
#### `response()` {#collection-method}
`response` 函數創建一個 [響應](/docs/{{version}}/responses) 實例,或者獲取響應工廠實例:
return response('Hello World', 200, $headers);
return response()->json(['foo' => 'bar'], 200, $headers);
<a name="method-retry"></a>
#### `retry()` {#collection-method}
`retry` 函數嘗試執行給定的回調,直到到達給定的最大嘗試次數。如果回調沒有派出異常并且有返回值則返回返回值。如果回調拋出異常,它將自動重試。如果超過最大嘗試次數,則拋出異常。
return retry(5, function () {
// 在 100ms 左右嘗試 5 次...
}, 100);
<a name="method-session"></a>
#### `session()` {#collection-method}
`session` 函數可以用來獲取或者設置 Session 值:
$value = session('key');
你可以通過健值對數組傳遞給函數來設置 Session 值:
session(['chairs' => 7, 'instruments' => 3]);
如果沒有傳遞值給函數,則返回 Session 實例:
$value = session()->get('key');
session()->put('key', $value);
<a name="method-tap"></a>
#### `tap()` {#collection-method}
`tap` 函數接受兩個參數:`$value` 和一個閉包。傳入的 `$value` 將會作為閉包函數的傳參,處理完后成為 `tap` 的返回值。閉包的返回值是無關緊要(不需要 `return` 關鍵詞)。
$user = tap(User::first(), function ($user) {
$user->name = 'taylor';
$user->save();
});
如果沒有傳遞閉包給 `tap` 函數,你可以調用給定 `$value` 上任何方法。不管方法中定義的實際返回值是什么,你調用的方法返回值始終 `$value`。例如,Eloquent `update` 一般返回一個整數。而我們可以通過 `tap` 函數鏈式調用 `update` 的方式返回模型本身:
$user = tap($user)->update([
'name' => $name,
'email' => $email
]);
<a name="method-value"></a>
#### `value()` {#collection-method}
`value` 函數可以簡單的返回它的值。然而,如果將 `閉包` 傳遞給函數,則運行這個 `閉包` 并返回結果:
$value = value(function () {
return 'bar';
});
<a name="method-view"></a>
#### `view()` {#collection-method}
`view` 函數獲取一個 [視圖](/docs/{{version}}/views) 實例:
return view('auth.login');
## 譯者署名
| 用戶名 | 頭像 | 職能 | 簽名 |
|---|---|---|---|
| [Seven Du](https://github.com/medz) | <img class="avatar-66 rm-style" src="https://avatars3.githubusercontent.com/u/5564821?s=300"> | 翻譯 | 基于 Laravel 的社交開源系統 [ThinkSNS+](https://github.com/slimkit/thinksns-plus) 歡迎 Star。 |
---
> {note} 歡迎任何形式的轉載,但請務必注明出處,尊重他人勞動共創開源社區。
>
> 轉載請注明:本文檔由 Laravel China 社區 [laravel-china.org](https://laravel-china.org) 組織翻譯,詳見 [翻譯召集帖](https://laravel-china.org/topics/5756/laravel-55-document-translation-call-come-and-join-the-translation)。
>
> 文檔永久地址: https://d.laravel-china.org
- 說明
- 翻譯說明
- 發行說明
- 升級說明
- 貢獻導引
- 入門指南
- 安裝
- 配置信息
- 文件夾結構
- HomeStead
- Valet
- 核心架構
- 請求周期
- 服務容器
- 服務提供者
- 門面(Facades)
- Contracts
- 基礎功能
- 路由
- 中間件
- CSRF 保護
- 控制器
- 請求
- 響應
- 視圖
- 重定向
- Session
- 表單驗證
- 錯誤與日志
- 前端開發
- Blade 模板
- 本地化
- 前端指南
- 編輯資源 Mix
- 安全
- 用戶認證
- API認證
- 用戶授權
- 加密解密
- 哈希
- 重置密碼
- 綜合話題
- Artisan 命令行
- 廣播系統
- 緩存系統
- 集合
- 事件系統
- 文件存儲
- 輔助函數
- 郵件發送
- 消息通知
- 擴展包開發
- 隊列
- 任務調度
- 數據庫
- 快速入門
- 查詢構造器
- 分頁
- 數據庫遷移
- 數據填充
- Redis
- Eloquent ORM
- 快速入門
- 模型關聯
- Eloquent 集合
- 修改器
- API 資源
- 序列化
- 測試
- 快速入門
- HTTP 測試
- 瀏覽器測試 Dusk
- 數據庫測試
- 測試模擬器
- 官方擴展包
- Cashier 交易工具包
- Envoy 部署工具
- Horizon
- Passport OAuth 認證
- Scout 全文搜索
- Socialite 社交化登錄
- 交流說明