# 版本
# 前言
我喜歡使用 Laravel 開發的感覺, 除了開發快速, 程式碼簡潔且優雅之外, Laravel 框架本身也是一個很好的學習參照物。 本篇主要將官方文件重點整理成 Q&A 的形式呈現, 原子化的概念, 這方式並不適用於每個人, 但若對你有幫助, 我會很開心。
# 目錄
# Method Listing
# Arrays & Objects
# Arr::accessible()
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
$isAccessible = Arr::accessible(['a' => 1, 'b' => 2]);
$isAccessible = Arr::accessible(new Collection);
$isAccessible = Arr::accessible('abc');
$isAccessible = Arr::accessible(new stdClass);
- Answer: 判斷 parameter 是否 array accessible
- Output:
<?php
// true
// true
// false
// false
# Arr::add()
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Arr;
$array = Arr::add(['name' => 'Desk'], 'price', 100);
$array = Arr::add(['name' => 'Desk', 'price' => null], 'price', 100);
- Answer: 將 key / value 加到 arg1 array 如果 given key 不存在於該 array, 或存在但其 value 為 null
- Output:
<?php
// ['name' => 'Desk', 'price' => 100]
// ['name' => 'Desk', 'price' => 100]
# Arr::collapse()
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Arr;
$array = Arr::collapse([[1, 2, 3], [4, 5, 6], [7, 8, 9]]);
- Answer: 將含有多個 arrays 的 array 變成一個 single array
- Output:
<?php
// [1, 2, 3, 4, 5, 6, 7, 8, 9]
# Arr::crossJoin()
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Arr;
$matrix = Arr::crossJoin([1, 2], ['a', 'b']);
$matrix = Arr::crossJoin([1, 2], ['a', 'b'], ['I', 'II']);
- Answer: cross join 帶入的 array
- Output:
<?php
/*
[
[1, 'a'],
[1, 'b'],
[2, 'a'],
[2, 'b'],
]
*/
/*
[
[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()
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Arr;
[$keys, $values] = Arr::divide(['name' => 'Desk']);
- Answer: 返回兩個 arrays, 一個裝 key, 一個裝 value
- Output:
<?php
// $keys: ['name']
// $values: ['Desk']
# Arr::dot()
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Arr;
$array = ['products' => ['desk' => ['price' => 100]]];
$flattened = Arr::dot($array);
- Answer: 將一個 multi-dimensional array 變成一個 single array, 並使用
. (英文句點符號)
來表示 depth - Output:
<?php
// ['products.desk.price' => 100]
# Arr::except()
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Arr;
$array = ['name' => 'Desk', 'price' => 100];
$filtered = Arr::except($array, ['price']);
- Answer: 將指定於 arg2 的 key 從 arg1 array 中移除
- Output:
<?php
// ['name' => 'Desk']
# Arr::exists()
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Arr;
$array = ['name' => 'John Doe', 'age' => 17];
$exists = Arr::exists($array, 'name');
$exists = Arr::exists($array, 'salary');
- Answer: 判斷 arg2 的 key 是否存在於 arg1 array
- Output:
<?php
// true
// false
# Array::first()
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Arr;
$array = [100, 200, 300];
$first = Arr::first($array, function ($value, $key) {
return $value >= 150;
});
- Answer: 回傳 arg1 array 中第一個符合 arg2 closure 中邏輯的輸出
- Output:
<?php
// 200
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Arr;
$first = Arr::first($array, $callback, $default);
- Answer: 回傳 arg1 array 中第一個符合 arg2 closure 中邏輯的輸出, 如果 array 中沒有出現符合的 item, 則 return arg3 $default
# Arr::flatten()
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Arr;
$array = ['name' => 'Joe', 'languages' => ['PHP', 'Ruby']];
$flattened = Arr::flatten($array);
- Answer: 將一個 multi-dimensional array 變為一個 single array
- Output:
<?php
// ['Joe', 'PHP', 'Ruby']
# Arr::forget()
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Arr;
$array = ['products' => ['desk' => ['price' => 100]]];
Arr::forget($array, 'products.desk');
- Answer: 將 arg2 指定 key 從 arg1 $array 中移除 可使用
. (英文句點符號)
來表示 depth - Output:
<?php
// ['products' => []]
# Arr::get()
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Arr;
$array = ['products' => ['desk' => ['price' => 100]]];
$price = Arr::get($array, 'products.desk.price');
- Answer: 從 arg1 $array 中取得 arg2 指定 key 的 value 可使用
. (英文句點符號)
來表示 depth - Output:
<?php
// 100
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Arr;
$discount = Arr::get($array, 'products.desk.discount', 0);
- Answer: 從 arg1 $array 中取得 arg2 指定 key 的 value 可使用
. (英文句點符號)
來表示 depth 如果沒有指定 key, 可指定 default 值 - Output:
<?php
// 0
# Arr::has()
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Arr;
$array = ['product' => ['name' => 'Desk', 'price' => 100]];
$contains = Arr::has($array, 'product.name');
$contains = Arr::has($array, ['product.price', 'product.discount']);
- Answer: 判斷 arg2 指定 key 是否存在於 arg1 $array 可使用
. (英文句點符號)
來表示 depth - Output:
<?php
// true
// false
# Arr::hasAny()
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Arr;
$array = ['product' => ['name' => 'Desk', 'price' => 100]];
$contains = Arr::hasAny($array, 'product.name');
$contains = Arr::hasAny($array, ['product.name', 'product.discount']);
$contains = Arr::hasAny($array, ['category', 'product.discount']);
- Answer: 判斷 arg2 指定的的多個 item 是否有任何一個存在於 arg1 $array
- Output:
<?php
// true
// true
// false
# Arr::isAssoc()
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Arr;
$isAssoc = Arr::isAssoc(['product' => ['name' => 'Desk', 'price' => 100]]);
$isAssoc = Arr::isAssoc([1, 2, 3]);
- Answer: 判斷帶入的 array 是否為一個 associative array 如果沒有從 0 開始的 sequential number 作為 key 的 array 都視為 associative array
- Output:
<?php
// true
// false
# Arr::last()
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Arr;
$array = [100, 200, 300, 110];
$last = Arr::last($array, function ($value, $key) {
return $value >= 150;
});
- Answer: 從 arg1 $array 中取得符合 arg2 closure, 從後面往前第一個值
- Output:
<?php
// 300
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Arr;
$last = Arr::last($array, $callback, $default);
- Answer: 從 arg1 $array 中取得符合 arg2 closure, 從後面往前第一個值 若無符合, 則回傳 default 值
# Arr::only()
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Arr;
$array = ['name' => 'Desk', 'price' => 100, 'orders' => 10];
$slice = Arr::only($array, ['name', 'price']);
- Answer: 從 arg1 $array 中, 取得符合 arg2 keys 的 item
- Output:
<?php
// ['name' => 'Desk', 'price' => 100]
# Arr::pluck()
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Arr;
$array = [
['developer' => ['id' => 1, 'name' => 'Taylor']],
['developer' => ['id' => 2, 'name' => 'Abigail']],
];
$names = Arr::pluck($array, 'developer.name');
- Answer: 從 arg1 $array 中, 取得所有符合 arg2 key 的 values 可使用
. (英文句點符號)
來表示 depth - Output:
<?php
// ['Taylor', 'Abigail']
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Arr;
$names = Arr::pluck($array, 'developer.name', 'developer.id');
- Answer: 從 arg1 $array 中, 取得所有符合 arg2 key 的 values, 可使用 arg3 定義 value 的 key 可使用
. (英文句點符號)
來表示 depth - Output:
<?php
// [1 => 'Taylor', 2 => 'Abigail']
# Arr::prepend()
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Arr;
$array = ['one', 'two', 'three', 'four'];
$array = Arr::prepend($array, 'zero');
- Answer: 將 arg2 的 item push 的 arg1 $array 的開始處
- Output:
<?php
// ['zero', 'one', 'two', 'three', 'four']
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Arr;
$array = ['price' => 100];
$array = Arr::prepend($array, 'Desk', 'name');
- Answer: 將 arg2 的 item push 的 arg1 $array 的開始處, 若有需要, 可使用 arg3 定義 value 的 key
- Output:
<?php
// ['name' => 'Desk', 'price' => 100]
# Arr::pull()
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Arr;
$array = ['name' => 'Desk', 'price' => 100];
$name = Arr::pull($array, 'name');
- Answer: 從 arg1 $array 中, 取出並同時從 $array 中移除符合 arg2 key 的 item
- Output:
<?php
// $name: Desk
// $array: ['price' => 100]
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Arr;
$value = Arr::pull($array, $key, $default);
- Answer: 從 arg1 $array 中, 取出並同時從 $array 中移除符合 arg2 key 的 item, 可定義 arg3 default 值, 當無符合的 item 時將回傳 default 值
# Arr::query()
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Arr;
$array = [
'name' => 'Taylor',
'order' => [
'column' => 'created_at',
'direction' => 'desc'
]
];
Arr::query($array);
- Answer: 將 parameter $array 轉為 query string
- Output:
<?php
// name=Taylor&order[column]=created_at&order[direction]=desc
# Arr::random()
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Arr;
$array = [1, 2, 3, 4, 5];
$random = Arr::random($array);
- Answer: 隨機 return $array 中的一個 value
- Output:
<?php
// 4 - (retrieved randomly)
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Arr;
$items = Arr::random($array, 2);
- Answer: 隨機 return $array 中的一個 value, arg2 可以指定要隨機 return value 的數量
- Output:
<?php
// [2, 5] - (retrieved randomly)
# Arr::set()
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Arr;
$array = ['products' => ['desk' => ['price' => 100]]];
Arr::set($array, 'products.desk.price', 200);
- Answer: 在 arg1 $array 中, set key / value = arg2 / arg3 可使用
. (英文句點符號)
來表示 depth - Output:
<?php
// ['products' => ['desk' => ['price' => 200]]]
# Arr::shuffle()
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Arr;
$array = Arr::shuffle([1, 2, 3, 4, 5]);
- Answer: 將 parameter array 順序隨機打亂
- Output:
<?php
// [3, 2, 5, 1, 4] - (generated randomly)
# Arr::sort()
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Arr;
$array = ['Desk', 'Table', 'Chair'];
$sorted = Arr::sort($array);
- Answer: sort $array
- Output:
<?php
// ['Chair', 'Desk', 'Table']
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Arr;
$array = [
['name' => 'Desk'],
['name' => 'Table'],
['name' => 'Chair'],
];
$sorted = array_values(Arr::sort($array, function ($value) {
return $value['name'];
}));
- Answer: 根據 closure 的回傳值來做 sorting
- Output:
<?php
/*
[
['name' => 'Chair'],
['name' => 'Desk'],
['name' => 'Table'],
]
*/
# Arr::sortRecursive()
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Arr;
$array = [
['Roman', 'Taylor', 'Li'],
['PHP', 'Ruby', 'JavaScript'],
['one' => 1, 'two' => 2, 'three' => 3],
];
$sorted = Arr::sortRecursive($array);
- Answer: recursively sort $array, 使用 sort() 排序 numerically indexed sub-arrays, 使用 ksort() 排序 associative sub-arrays
- Output:
<?php
/*
[
['JavaScript', 'PHP', 'Ruby'],
['one' => 1, 'three' => 3, 'two' => 2],
['Li', 'Roman', 'Taylor'],
]
*/
# Arr::where()
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Arr;
$array = [100, '200', 300, '400', 500];
$filtered = Arr::where($array, function ($value, $key) {
return is_string($value);
});
- Answer: 使用 arg2 closure 過濾 arg1 $array
- Output:
<?php
// [1 => '200', 3 => '400']
# Arr::wrap()
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Arr;
$string = 'Laravel';
$array = Arr::wrap($string);
- Answer: 會用 array 將 given parameter 包起來, 如果 given parameter 已經是 array, 那則不變
- Output:
<?php
// ['Laravel']
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Arr;
$array = Arr::wrap(null);
- Answer: 如果 given parameter 為 null, 則 return empty array
- Output:
<?php
// []
# data_fill()
以下的 Laravel example code 的意思是?
- Example:
<?php
$data = ['products' => ['desk' => ['price' => 100]]];
data_fill($data, 'products.desk.price', 200);
data_fill($data, 'products.desk.discount', 10);
- Answer: 使用
'dot'
notation 代表 depth, 將 arg2 key / arg3 value fill 到 arg1 $data (如果不存在的話), $data 可以是 array 或 object 與 data_set 的差異在於, 如果指定 key 已存在, data_fill 不會覆蓋, data_set 會 - Output:
<?php
// ['products' => ['desk' => ['price' => 100]]]
// ['products' => ['desk' => ['price' => 100, 'discount' => 10]]]
以下的 Laravel example code 的意思是?
- Example:
<?php
$data = [
'products' => [
['name' => 'Desk 1', 'price' => 100],
['name' => 'Desk 2'],
],
];
data_fill($data, 'products.*.price', 200);
- Answer: 使用
'dot'
notation 代表 depth, 將 arg2 key / arg3 value set 到 arg1 $data (如果不存在的話), $data 可以是 array 或 object, 也可使用*
來代表 wildcard, 一次性設定多個 sub-array 中的 key - Output:
<?php
/*
[
'products' => [
['name' => 'Desk 1', 'price' => 100],
['name' => 'Desk 2', 'price' => 200],
],
]
*/
# data_get()
以下的 Laravel example code 的意思是?
- Example:
<?php
$data = ['products' => ['desk' => ['price' => 100]]];
$price = data_get($data, 'products.desk.price');
- Answer: 從 arg1 $data 中取得 arg2 指定的 item, 使用
. (英文句點符號)
來表示 depth, $data 可以是 array 或 object - Output:
<?php
// 100
以下的 Laravel example code 的意思是?
- Example:
<?php
$discount = data_get($data, 'products.desk.discount', 0);
// 0
- Answer: 從 arg1 $data 中取得 arg2 指定的 item, 使用
. (英文句點符號)
來表示 depth, $data 可以是 array 或 object, 也可在 arg3 定義 default 值, 當 arg2 item 不存在時 return default 值
以下的 Laravel example code 的意思是?
- Example:
<?php
$data = [
'product-one' => ['name' => 'Desk 1', 'price' => 100],
'product-two' => ['name' => 'Desk 2', 'price' => 150],
];
data_get($data, '*.name');
- Answer: 從 arg1 $data 中取得 arg2 指定的 item, 使用
. (英文句點符號)
來表示 depth, $data 可以是 array 或 object, 也可使用* (星號)
表示 wildcard, 取出多個 sub-arrays 中的指定 item - Output:
<?php
// ['Desk 1', 'Desk 2'];
# data_set()
以下的 Laravel example code 的意思是?
- Example:
<?php
$data = ['products' => ['desk' => ['price' => 100]]];
data_set($data, 'products.desk.price', 200);
- Answer: 使用
'dot'
notation 代表 depth, 將 arg2 key / arg3 value set 到 arg1 $data (如果不存在的話), $data 可以是 array 或 object 與 data_set 的差異在於, 如果指定 key 已存在, data_fill 不會覆蓋, data_set 會 - Output:
<?php
// ['products' => ['desk' => ['price' => 200]]]
以下的 Laravel example code 的意思是?
- Example:
<?php
$data = [
'products' => [
['name' => 'Desk 1', 'price' => 100],
['name' => 'Desk 2', 'price' => 150],
],
];
data_set($data, 'products.*.price', 200);
- Answer: 使用
'dot'
notation 代表 depth, 將 arg2 key / arg3 value set 到 arg1 $data (如果不存在的話), $data 可以是 array 或 object 也可使用* (星號)
表示 wildcard, 在多個 sub-arrays 中, set 指定 item 與 data_set 的差異在於, 如果指定 key 已存在, data_fill 不會覆蓋, data_set 會 - Output:
<?php
/*
[
'products' => [
['name' => 'Desk 1', 'price' => 200],
['name' => 'Desk 2', 'price' => 200],
],
]
*/
以下的 Laravel example code 的意思是?
- Example:
<?php
$data = ['products' => ['desk' => ['price' => 100]]];
data_set($data, 'products.desk.price', 200, $overwrite = false);
- Answer: 使用
'dot'
notation 代表 depth, 將 arg2 key / arg3 value set 到 arg1 $data (如果不存在的話), $data 可以是 array 或 object 與 data_set 的差異在於, 如果指定 key 已存在, data_fill 不會覆蓋, data_set 會 data_set 預設會複寫, 也可在 arg4 帶入$overwrite = false
, 這樣就不會複寫已存在的 key - Output:
<?php
// ['products' => ['desk' => ['price' => 100]]]
# head()
以下的 Laravel example code 的意思是?
- Example:
<?php
$array = [100, 200, 300];
$first = head($array);
- Answer: return 帶入 array 中的第一個 item
- Output:
<?php
// 100
# last()
以下的 Laravel example code 的意思是?
- Example:
<?php
$array = [100, 200, 300];
$last = last($array);
- Answer: return given array 中的最後一個 item
- Output:
<?php
// 300
# Paths
以下的 Laravel example code 的意思是?
- Example:
<?php
$path = app_path();
$path = app_path('Http/Controllers/Controller.php');
- Answer: 取得通往 project 中, app 資料夾的系統絕對路徑, 比如說你的 project 放在 /var/www/projects 中, 那路徑就會是
/var/www/projects/yourProjectName/app
, 也可帶入 app 資料夾中的任何檔案相對位置
以下的 Laravel example code 的意思是?
- Example:
<?php
$path = base_path();
$path = base_path('vendor/bin');
- Answer: 取得通往 project 的系統絕對路徑, 比如說你的 project 放在 /var/www/projects 中, 那路徑就會是
/var/www/projects/yourProjectName
, 也可帶入 project 中的任何檔案相對位置
以下的 Laravel example code 的意思是?
- Example:
<?php
$path = config_path();
$path = config_path('app.php');
- Answer: 取得通往 project 的 config 資料夾系統絕對路徑, 比如說你的 project 放在 /var/www/projects 中, 那路徑就會是
/var/www/projects/yourProjectName/config
, 也可帶入 config 資料夾中的任何檔案相對位置
以下的 Laravel example code 的意思是?
- Example:
<?php
$path = database_path();
$path = database_path('factories/UserFactory.php');
- Answer: 取得通往 project 的 database 資料夾系統絕對路徑, 比如說你的 project 放在 /var/www/projects 中, 那路徑就會是
/var/www/projects/yourProjectName/database
, 也可帶入 database 資料夾中的任何檔案相對位置
以下的 Laravel example code 的意思是?
- Example:
<?php
$path = mix('css/app.css');
- Answer: 取得 project mix file
以下的 Laravel example code 的意思是?
- Example:
<?php
$path = public_path();
$path = public_path('css/app.css');
- Answer: 取得通往 project 的 public 資料夾系統絕對路徑, 比如說你的 project 放在 /var/www/projects 中, 那路徑就會是
/var/www/projects/yourProjectName/public
, 也可帶入 public 資料夾中的任何檔案相對位置
以下的 Laravel example code 的意思是?
- Example:
<?php
$path = resource_path();
$path = resource_path('sass/app.scss');
- Answer: 取得通往 project 的 resources 資料夾系統絕對路徑, 比如說你的 project 放在 /var/www/projects 中, 那路徑就會是
/var/www/projects/yourProjectName/resources
, 也可帶入 resources 資料夾中的任何檔案相對位置
以下的 Laravel example code 的意思是?
- Example:
<?php
$path = storage_path();
$path = storage_path('app/file.txt');
- Answer: 取得通往 project 的 storage 資料夾系統絕對路徑, 比如說你的 project 放在 /var/www/projects 中, 那路徑就會是
/var/www/projects/yourProjectName/storage
, 也可帶入 storage 資料夾中的任何檔案相對位置
以下的 Laravel example code 的意思是?
- Example:
<?php
echo __('Welcome to our application');
echo __('messages.welcome');
- Answer:
__
function 會使用 localization file 來 translate 帶入的 translation string 或 key, 如果該 translation string 或 key 不存在, 則 return 帶入的 string
以下的 Laravel example code 的意思是?
- Example:
<?php
$class = class_basename('Foo\Bar\Baz');
// Baz
- Answer: class_basename() 可以取得 class name without namespace
以下的 Laravel example code 的意思是?
- Example:
<?php
echo e('<html>foo</html>');
// <html>foo</html>
- Answer: e() 會執行 PHP
htmlspecialchars
function, 預設 double_encode option
以下的 Laravel example code 的意思是?
- Example:
<?php
$string = 'The event will take place between :start and :end';
$replaced = preg_replace_array('/:[a-z_]+/', ['8:30', '9:00'], $string);
// The event will take place between 8:30 and 9:00
- Answer: 在 arg3 $string 中尋找符合 arg1 pattern 的 string, 然後依照順序使用 arg2 array 中的 item 取代
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Str;
$slice = Str::after('This is my name', 'This is');
// ' my name'
- Answer: 在 arg1 string 中, 尋找 arg2 的字段, 並 return arg2 之後的字段
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Str;
$slice = Str::afterLast('App\Http\Controllers\Controller', '\\');
// 'Controller'
- Answer: 從 arg1 string 中尋找 arg2 的 string 最後一次出現的地方, 然後 return arg2 string 之後的 string, 如果沒找到 arg2 string, 則回傳整個 arg1 string
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Str;
$slice = Str::ascii('û');
- Answer: 將 arg 移譯成 ASCII value // ‘u’
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Str;
$slice = Str::before('This is my name', 'my name');
- Answer: 在 arg1 string 中, 尋找 arg2 的字段, 並 return arg2 之前的字段 // ‘This is ‘
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Str;
$slice = Str::beforeLast('This is my name', 'is');
// 'This '
- Answer: 在 arg1 string 中, 尋找 arg2 的字段, 並 return arg2 最後一次出現時前面的所有字段
以下的 Laravel example code 的意思是?
- Example:
<?php
$slice = Str::between('This is my name', 'This', 'name');
- Answer: 從 arg1 string 中, 取得 arg2, arg3 中間的 string // ‘ is my ‘
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Str;
$converted = Str::camel('foo_bar');
- Answer: 將 string 轉為 camel case // fooBar
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Str;
$contains = Str::contains('This is my name', 'my');
- Answer: 判斷 arg1 string 是否含有 arg2 string // true
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Str;
$contains = Str::contains('This is my name', ['my', 'foo']);
- Answer: 判斷 arg1 string 內, 是否含有 arg2 array 內的任何一個 string // true
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Str;
$containsAll = Str::containsAll('This is my name', ['my', 'name']);
- Answer: 判斷 arg1 string 中, 是否包含 arg2 array 中的所有 string // true
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Str;
$result = Str::endsWith('This is my name', 'name');
- Answer: 判斷 arg1 string 是否以 arg2 string 結尾 // true
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Str;
$result = Str::endsWith('This is my name', ['name', 'foo']);
$result = Str::endsWith('This is my name', ['this', 'foo']);
- Answer: 判斷 arg1 string 是否以 arg2 array 中的任何一個 string 結尾 // true // false
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Str;
$adjusted = Str::finish('this/string', '/');
$adjusted = Str::finish('this/string/', '/');
- Answer: 如果 arg1 string 並非以 arg2 string 結尾, 則加上 // this/string/ // this/string/
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Str;
$matches = Str::is('foo*', 'foobar');
$matches = Str::is('baz*', 'foobar');
- Answer: 判斷 arg2 是否符合 arg1 pattern // true // false
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Str;
$isAscii = Str::isAscii('Taylor');
$isAscii = Str::isAscii('ü');
- Answer: 判斷 string 是否為 7 bit ASCII // true // false
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Str;
$isUuid = Str::isUuid('a0a2a2d2-0b87-4a18-83f2-2529882be2de');
$isUuid = Str::isUuid('laravel');
- Answer: 判斷 string 是否為 valid uuid // true // false
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Str;
$converted = Str::kebab('fooBar');
- Answer: 判斷 string 是否為 kebab case // foo-bar
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Str;
$length = Str::length('Laravel');
- Answer: 取得 string length // 7
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Str;
$truncated = Str::limit('The quick brown fox jumps over the lazy dog', 20);
- Answer: 將 arg1 string 從頭開始只取 arg2 給的字串數, 其餘截掉 // The quick brown fox…
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Str;
$truncated = Str::limit('The quick brown fox jumps over the lazy dog', 20, ' (...)');
- Answer: 將 arg1 string 從頭開始只取 arg2 給的字串數, 其餘截掉, 可帶入 arg3 取代被截掉的部分 // The quick brown fox (…)
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Str;
$converted = Str::lower('LARAVEL');
- Answer: 取得 string 的 lower case // laravel
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Str;
$html = Str::markdown('# Laravel');
// <h1>Laravel</h1>
$html = Str::markdown('# Taylor <b>Otwell</b>', [
'html_input' => 'strip',
]);
// <h1>Taylor Otwell</h1>
- Answer: 將 markdown 格式轉為 HTML
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Str;
return (string) Str::orderedUuid();
- Answer: 產生 timestamp first UUID, 凡是使用此 method 的 uuid, 會依照順序產生
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Str;
$padded = Str::padBoth('James', 10, '_');
$padded = Str::padBoth('James', 10);
- Answer: 使用 PHP function
str_pad
, 會依序在左右開始加上 arg3 string, 直到 string 長度達到 arg2 指定的長度 //'__James___'
//' James '
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Str;
$padded = Str::padLeft('James', 10, '-=');
// '-=-=-James'
$padded = Str::padLeft('James', 10);
// ' James'
- Answer: 會使用 PHP function
str_pad
, 從 arg1 string 的左側開始 pad arg3 的 string, 直到長度達到 arg2 指定的長度
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Str;
$padded = Str::padRight('James', 10, '-');
$padded = Str::padRight('James', 10);
- Answer: 會使用 PHP function
str_pad
, 從 arg1 string 的右側開始 pad arg3 的 string, 直到長度達到 arg2 指定的長度 //'James-----'
//'James '
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Str;
$plural = Str::plural('car');
$plural = Str::plural('child');
- Answer: 將 string 轉為其複數 // cars // children
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Str;
$plural = Str::plural('child', 2);
$singular = Str::plural('child', 1);
- Answer: 根據 arg2 的 integer, 將 string 轉為其單數或複數 // children // child
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Str;
$plural = Str::pluralStudly('VerifiedHuman');
$plural = Str::pluralStudly('UserFeedback');
- Answer: 將 Studly caps case string 轉為單字複數 // VerifiedHumans // UserFeedback
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Str;
$plural = Str::pluralStudly('VerifiedHuman', 2);
// VerifiedHumans
$singular = Str::pluralStudly('VerifiedHuman', 1);
// VerifiedHuman
- Answer: 依據 arg2 的 integer 將 Studly caps case string 轉為單字單數或複數
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Str;
$random = Str::random(40);
- Answer: 產生一段指定長度的 random string
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Str;
$string = 'The event will take place between ? and ?';
$replaced = Str::replaceArray('?', ['8:30', '9:00'], $string);
- Answer: 從 arg3 string 中, 尋找 arg1 的字串, 並將之以 arg2 array 中的字串依序取代 // The event will take place between 8:30 and 9:00
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Str;
$replaced = Str::replaceFirst('the', 'a', 'the quick brown fox jumps over the lazy dog');
// a quick brown fox jumps over the lazy dog
- Answer: 從 arg3 string 中尋找 arg1 字串, 並以 arg2 字串取代之
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Str;
$replaced = Str::replaceLast('the', 'a', 'the quick brown fox jumps over the lazy dog');
- Answer: 在 arg3 string 中, 尋找 arg1 string 最後出現的位置, 並以 arg2 string 取代之 // the quick brown fox jumps over a lazy dog
以下的 Laravel example code 的意思是?
- Example:
<?use Illuminate\Support\Str;
$singular = Str::singular('cars');
$singular = Str::singular('children');
- Answer: 取得單字的單數型態 // car // childphp
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Str;
$slug = Str::slug('Laravel 5 Framework', '-');
- Answer: 使用 arg2 的 string, 為 arg1 產生一個 URL friendly slug // laravel-5-framework
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Str;
$converted = Str::snake('fooBar');
- Answer: 將 string 轉為 snake case // foo_bar
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Str;
$adjusted = Str::start('this/string', '/');
$adjusted = Str::start('/this/string', '/');
- Answer: 如果 arg1 string 不是由 arg2 開頭的話, 就加上去 // /this/string // /this/string
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Str;
$result = Str::startsWith('This is my name', 'This');
- Answer: 判斷 arg1 string 是否 start with arg1 string // true
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Str;
$converted = Str::studly('foo_bar');
- Answer: 將 string 轉為 StudlyCase // FooBar
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Str;
$converted = Str::substr('The Laravel Framework', 4, 7);
- Answer: 從 arg1 string 中, 從 arg2 index 位置開始往後取, 取 arg3 指定的 length // Laravel
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Str;
$count = Str::substrCount('If you like ice cream, you will like snow cones.', 'like');
- Answer: 取得在 arg1 string 中共出現了 arg2 string 幾次 // 2
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Str;
$converted = Str::title('a nice title uses the correct case');
- Answer: 將 string 轉為 Title Case // A Nice Title Uses The Correct Case
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Str;
$string = Str::ucfirst('foo bar');
- Answer: 將 string 第一個字母轉為大寫 // Foo bar
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Str;
$string = Str::upper('laravel');
- Answer: 將整個 string 中的每個字母都轉為大寫 // LARAVEL
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Str;
return (string) Str::uuid();
- Answer: 產生 version 4 UUID
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Str;
return Str::words('Perfectly balanced, as all things should be.', 3, ' >>>');
- Answer: 將 arg1 string, 使用 arg3 的數字來限制單字數量, arg3 代表之後刪減掉的部分要顯示什麼 // Perfectly balanced, as >>>
以下的 Laravel example code 的意思是?
- Example:
<?php
echo trans('messages.welcome');
- Answer: 使用 localization file 來翻譯指定的 translation key, 如果指定的 translation key 不存在, 將 return 帶入的 key, 如果 message.welcome 這個 translation key 不存在, 將回傳
messages.welcome
以下的 Laravel example code 的意思是?
- Example:
<?php
echo trans_choice('messages.notifications', $unreadCount);
- Answer: 使用 localization file 來翻譯指定的 translation key, 如果指定的 translation key 不存在, 將會 return 帶入的 key, 如果 message.welcome 這個 translation key 不存在, 將回傳
messages.welcome
arg2 為用來替換 translation value 的值, 比方說The unread number is :unreadCount
# Fluent Strings
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Str;
$slice = Str::of('This is my name')->after('This is');
- Answer: // ‘ my name’ Fluent string 語法, 可以 chain 多個 string operations, 使其更加物件導向以及語意化 在 of() 中的 string 中尋找帶入 after() 中的 string, 並取其後面的 string, 如果 after() 中的 string 不存在, 會 return of() 中的所有 string
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Str;
$slice = Str::of('App\Http\Controllers\Controller')->afterLast('\\');
- Answer: Fluent string 語法, 可以 chain 多個 string operations, 使其更加物件導向以及語意化 // ‘Controller’ return
\\
在 of() string 中最後出現的位置其後方的 string
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Str;
$string = Str::of('Taylor')->append(' Otwell');
- Answer: Fluent string 語法, 可以 chain 多個 string operations, 使其更加物件導向以及語意化 // ‘Taylor Otwell’ 將 append() string append 到 of() string
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Str;
$string = Str::of('ü')->ascii();
- Answer: Fluent string 語法, 可以 chain 多個 string operations, 使其更加物件導向以及語意化 // ‘u’ 將 of() string 轉音譯為 ASCII value
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Str;
$string = Str::of('/foo/bar/baz')->basename();
- Answer: Fluent string 語法, 可以 chain 多個 string operations, 使其更加物件導向以及語意化 // ‘baz’ 取得 of string 的 basename
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Str;
$string = Str::of('/foo/bar/baz.jpg')->basename('.jpg');
- Answer: Fluent string 語法, 可以 chain 多個 string operations, 使其更加物件導向以及語意化 // ‘baz’ 取得 of string 的 basename, 會從 result 中移除 basename() string
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Str;
$slice = Str::of('This is my name')->before('my name');
- Answer: Fluent string 語法, 可以 chain 多個 string operations, 使其更加物件導向以及語意化 // ‘This is ‘ 從 of() string 中, 尋找 before() string, 並取其前面的 string
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Str;
$slice = Str::of('This is my name')->beforeLast('is');
- Answer: Fluent string 語法, 可以 chain 多個 string operations, 使其更加物件導向以及語意化 // ‘This ‘ 從 of() string 中尋找 before() string 最後一次出現的地方, 並取其前面的 string
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Str;
$converted = Str::of('foo_bar')->camel();
- Answer: Fluent string 語法, 可以 chain 多個 string operations, 使其更加物件導向以及語意化 // fooBar 將 of() string 轉為 camel case
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Str;
$contains = Str::of('This is my name')->contains('my');
- Answer: Fluent string 語法, 可以 chain 多個 string operations, 使其更加物件導向以及語意化 // true 判斷 of() string 中是否含有 contains() string
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Str;
$contains = Str::of('This is my name')->contains(['my', 'foo']);
- Answer: Fluent string 語法, 可以 chain 多個 string operations, 使其更加物件導向以及語意化 // true 判斷 of() string 中是否含有 contains() string, 只要中一個就算
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Str;
$containsAll = Str::of('This is my name')->containsAll(['my', 'name']);
- Answer: Fluent string 語法, 可以 chain 多個 string operations, 使其更加物件導向以及語意化 // true 判斷 of() string 中是否含有 contains() string, 全中才算
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Str;
$string = Str::of('/foo/bar/baz')->dirname();
- Answer: Fluent string 語法, 可以 chain 多個 string operations, 使其更加物件導向以及語意化 // ‘/foo/bar’ 取 of() string 的 dirname
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Str;
$string = Str::of('/foo/bar/baz')->dirname(2);
- Answer: Fluent string 語法, 可以 chain 多個 string operations, 使其更加物件導向以及語意化 // ‘/foo’ 取 of() string 的 dirname, 可帶入 dirname() 你希望 trim 的 dir level 數量
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Str;
$result = Str::of('This is my name')->endsWith('name');
- Answer: Fluent string 語法, 可以 chain 多個 string operations, 使其更加物件導向以及語意化 // true 判斷 of() string 是否以 endsWith() string 結尾
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Str;
$result = Str::of('This is my name')->endsWith(['name', 'foo']);
$result = Str::of('This is my name')->endsWith(['this', 'foo']);
- Answer: Fluent string 語法, 可以 chain 多個 string operations, 使其更加物件導向以及語意化 // true // false 判斷 of() string 是否以 endsWith() string 結尾, 中一個就算
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Str;
$result = Str::of('Laravel')->exactly('Laravel');
- Answer: Fluent string 語法, 可以 chain 多個 string operations, 使其更加物件導向以及語意化 // true 判斷 of() string 是否跟 exactly() string 完全相同
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Str;
$collection = Str::of('foo bar baz')->explode(' ');
- Answer: Fluent string 語法, 可以 chain 多個 string operations, 使其更加物件導向以及語意化 //
collect(['foo', 'bar', 'baz'])
將 of() string 以 explode() string 為 delimiter, 拆成一個 array
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Str;
$adjusted = Str::of('this/string')->finish('/');
$adjusted = Str::of('this/string/')->finish('/');
- Answer: Fluent string 語法, 可以 chain 多個 string operations, 使其更加物件導向以及語意化 // this/string/ // this/string/ 如果 of() string 不是以 finish() string 結尾, 則加上
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Str;
$matches = Str::of('foobar')->is('foo*');
$matches = Str::of('foobar')->is('baz*');
- Answer: Fluent string 語法, 可以 chain 多個 string operations, 使其更加物件導向以及語意化 // true // false 判斷 of() string 是否符合 is() pattern
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Str;
$result = Str::of('Taylor')->isAscii();
$result = Str::of('ü')->isAscii();
- Answer: Fluent string 語法, 可以 chain 多個 string operations, 使其更加物件導向以及語意化 // true // false 判斷 of() string 是否為 ASCII value
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Str;
$result = Str::of(' ')->trim()->isEmpty();
$result = Str::of('Laravel')->trim()->isEmpty();
- Answer: Fluent string 語法, 可以 chain 多個 string operations, 使其更加物件導向以及語意化 // true // false 判斷 of() string 是否 empty()
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Str;
$result = Str::of(' ')->trim()->isNotEmpty();
$result = Str::of('Laravel')->trim()->isNotEmpty();
- Answer: Fluent string 語法, 可以 chain 多個 string operations, 使其更加物件導向以及語意化 // false // true 判斷 of() string 是否 isNotempty()
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Str;
$converted = Str::of('fooBar')->kebab();
- Answer: Fluent string 語法, 可以 chain 多個 string operations, 使其更加物件導向以及語意化 // foo-bar 將 of() string 轉為 kebab case
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Str;
$length = Str::of('Laravel')->length();
- Answer: Fluent string 語法, 可以 chain 多個 string operations, 使其更加物件導向以及語意化 // 7 取得 of() string 的 length
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Str;
$truncated = Str::of('The quick brown fox jumps over the lazy dog')->limit(20);
- Answer: Fluent string 語法, 可以 chain 多個 string operations, 使其更加物件導向以及語意化 // The quick brown fox… 限制 of() string 的 length, 超過則截斷
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Str;
$truncated = Str::of('The quick brown fox jumps over the lazy dog')->limit(20, ' (...)');
- Answer: Fluent string 語法, 可以 chain 多個 string operations, 使其更加物件導向以及語意化 // The quick brown fox (…) 限制 of() string 的 length, 超過則截斷, 並顯示 limit() arg2 的 string
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Str;
$result = Str::of('LARAVEL')->lower();
- Answer: Fluent string 語法, 可以 chain 多個 string operations, 使其更加物件導向以及語意化 // ‘laravel’ 將 of() string 轉為 lower case
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Str;
$string = Str::of(' Laravel ')->ltrim();
$string = Str::of('/Laravel/')->ltrim('/');
- Answer: Fluent string 語法, 可以 chain 多個 string operations, 使其更加物件導向以及語意化 //
'Laravel '
//'Laravel/'
trim of() string 的 left side
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Str;
$html = Str::of('# Laravel')->markdown();
$html = Str::of('# Taylor <b>Otwell</b>')->markdown([
'html_input' => 'strip',
]);
- Answer: Fluent string 語法, 可以 chain 多個 string operations, 使其更加物件導向以及語意化 //
<h1>Laravel</h1>
//<h1>Taylor Otwell</h1>
將 of() string 中如果有 markdown 語法, 轉為 HTML 效果
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Str;
$result = Str::of('foo bar')->match('/bar/');
$result = Str::of('foo bar')->match('/foo (.*)/');
- Answer: Fluent string 語法, 可以 chain 多個 string operations, 使其更加物件導向以及語意化 // ‘bar’ // ‘bar’ 回傳 of() string 中, 符合 match() 中 regex pattern 的部分, 如果沒有符合, 則回傳 empty collection
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Str;
$result = Str::of('bar foo bar')->matchAll('/bar/');
- Answer: Fluent string 語法, 可以 chain 多個 string operations, 使其更加物件導向以及語意化 //
collect(['bar', 'bar'])
回傳一個 collection, 含有 of() string 中, 符合 matchAll() 中 regex pattern 的全部 string
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Str;
$result = Str::of('bar fun bar fly')->matchAll('/f(\w*)/');
- Answer: Fluent string 語法, 可以 chain 多個 string operations, 使其更加物件導向以及語意化 //
collect(['un', 'ly']);
回傳一個 collection, 含有 of() string 中, 符合 matchAll() 中 regex pattern 的全部 string, 如果 pattern 含有 group, 會回傳符合該 group 的 string, 如果沒有符合, 則回傳 empty collection
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Str;
$padded = Str::of('James')->padBoth(10, '_');
$padded = Str::of('James')->padBoth(10);
- Answer: Fluent string 語法, 可以 chain 多個 string operations, 使其更加物件導向以及語意化 //
'__James___'
//' James '
使用 padBoth(), 平均 pad of() string 的左右兩邊, 直到滿足 padBoth() arg1 length, 若有指定 arg2, 則使用該 string 補滿, 若無則使用空白
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Str;
$padded = Str::of('James')->padLeft(10, '-=');
$padded = Str::of('James')->padLeft(10);
- Answer: Fluent string 語法, 可以 chain 多個 string operations, 使其更加物件導向以及語意化 //
'-=-=-James'
//' James'
使用 padLeft(), 補滿 pad of() string 的左邊, 直到滿足 padLeft() arg1 length, 若有指定 arg2, 則使用該 string 補滿, 若無則使用空白
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Str;
$padded = Str::of('James')->padRight(10, '-');
$padded = Str::of('James')->padRight(10);
- Answer: Fluent string 語法, 可以 chain 多個 string operations, 使其更加物件導向以及語意化 //
'James-----'
//'James '
使用 padRight(), 補滿 pad of() string 的右邊, 直到滿足 padRight() arg1 length, 若有指定 arg2, 則使用該 string 補滿, 若無則使用空白
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Str;
$hash = Str::of('Laravel')->pipe('md5')->prepend('Checksum: ');
$closure = Str::of('foo')->pipe(function ($str) {
return 'bar';
});
- Answer: Fluent string 語法, 可以 chain 多個 string operations, 使其更加物件導向以及語意化 // ‘Checksum: a5c95b86291ea299fcbe64458ed12702’ // ‘bar’ 可以 transform of() string, 藉由將 string 帶入 closure
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Str;
$plural = Str::of('car')->plural();
$plural = Str::of('child')->plural();
- Answer: Fluent string 語法, 可以 chain 多個 string operations, 使其更加物件導向以及語意化 // cars // children 將 of() string 轉為複數
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Str;
$plural = Str::of('child')->plural(2);
// children
$plural = Str::of('child')->plural(1);
// child
- Answer: Fluent string 語法, 可以 chain 多個 string operations, 使其更加物件導向以及語意化 將 of() string 轉為單數或複數, 藉由帶入 plural() 數字
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Str;
$string = Str::of('Framework')->prepend('Laravel ');
- Answer: Fluent string 語法, 可以 chain 多個 string operations, 使其更加物件導向以及語意化 // Laravel Framework 將 prepend() string 置於 of() string 之前
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Str;
$replaced = Str::of('Laravel 6.x')->replace('6.x', '7.x');
- Answer: Fluent string 語法, 可以 chain 多個 string operations, 使其更加物件導向以及語意化 // Laravel 7.x 將 of() string 中的 repace() arg1 string, replace 為 replace() arg2 string
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Str;
$string = 'The event will take place between ? and ?';
$replaced = Str::of($string)->replaceArray('?', ['8:30', '9:00']);
- Answer: Fluent string 語法, 可以 chain 多個 string operations, 使其更加物件導向以及語意化 // The event will take place between 8:30 and 9:00 在 of() string 中尋找 replaceArray() arg1 的 string, 將將之依序替換為 arg2 array 中的 string
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Str;
$replaced = Str::of('the quick brown fox jumps over the lazy dog')->replaceFirst('the', 'a');
- Answer: Fluent string 語法, 可以 chain 多個 string operations, 使其更加物件導向以及語意化 // a quick brown fox jumps over the lazy dog 從 of() string 中尋找 replaceFirst() arg1 string, 將找到的第一個替換為 replaceFirst() arg2 string
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Str;
$replaced = Str::of('the quick brown fox jumps over the lazy dog')->replaceLast('the', 'a');
- Answer: Fluent string 語法, 可以 chain 多個 string operations, 使其更加物件導向以及語意化 // the quick brown fox jumps over a lazy dog 從 of() string 中尋找 replaceLast arg1 string, 將找到的最後一個替換成 replaceLast() arg2 string
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Str;
$replaced = Str::of('(+1) 501-555-1000')->replaceMatches('/[^A-Za-z0-9]++/', '')
- Answer: Fluent string 語法, 可以 chain 多個 string operations, 使其更加物件導向以及語意化 // ‘15015551000’ 將 of() string 中, 符合 replaceMatches() arg1 pattern 的部分替換成 replaceMatches() arg2 string
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Str;
$result = Str::of('This is my name')->endsWith('name');
- Answer: Fluent string 語法, 可以 chain 多個 string operations, 使其更加物件導向以及語意化 // true 判斷 of() string 否則以 endWith() string 結尾
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Str;
$result = Str::of('This is my name')->endsWith(['name', 'foo']);
$result = Str::of('This is my name')->endsWith(['this', 'foo']);
- Answer: Fluent string 語法, 可以 chain 多個 string operations, 使其更加物件導向以及語意化 // true // false 判斷 of() string 否則以 endWith() string 結尾, 一個中就算
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Str;
$plural = Str::of('car')->plural();
$plural = Str::of('child')->plural();
- Answer: Fluent string 語法, 可以 chain 多個 string operations, 使其更加物件導向以及語意化 // cars // children 取得 of() string 的複數
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Str;
$plural = Str::of('child')->plural(2);
$plural = Str::of('child')->plural(1);
- Answer: Fluent string 語法, 可以 chain 多個 string operations, 使其更加物件導向以及語意化 // children // child 可帶入 plural() 數字, 決定要 return of() string 的單複數
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Str;
$string = Str::of(' Laravel ')->rtrim();
$string = Str::of('/Laravel/')->rtrim('/');
- Answer: Fluent string 語法, 可以 chain 多個 string operations, 使其更加物件導向以及語意化 // ‘ Laravel’ // ‘/Laravel’ trim of() string 的右邊, 可帶入想要 trim 的字串
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Str;
$singular = Str::of('cars')->singular();
$singular = Str::of('children')->singular();
- Answer: Fluent string 語法, 可以 chain 多個 string operations, 使其更加物件導向以及語意化 // car // child 將 of() string 轉為單數
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Str;
$slug = Str::of('Laravel Framework')->slug('-');
- Answer: Fluent string 語法, 可以 chain 多個 string operations, 使其更加物件導向以及語意化 // laravel-framework 從 of() string 產生 URL friendly slug
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Str;
$converted = Str::of('fooBar')->snake();
- Answer: Fluent string 語法, 可以 chain 多個 string operations, 使其更加物件導向以及語意化 // foo_bar return of() string 的 snake case
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Str;
$segments = Str::of('one, two, three')->split('/[\s,]+/');
- Answer: Fluent string 語法, 可以 chain 多個 string operations, 使其更加物件導向以及語意化 //
collect(["one", "two", "three"])
使用 split() pattern 來 split of() string
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Str;
$adjusted = Str::of('this/string')->start('/');
$adjusted = Str::of('/this/string')->start('/');
- Answer: Fluent string 語法, 可以 chain 多個 string operations, 使其更加物件導向以及語意化 // /this/string // /this/string 如果 of() string 不是以 start() string 開頭, 則加上
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Str;
$result = Str::of('This is my name')->startsWith('This');
- Answer: Fluent string 語法, 可以 chain 多個 string operations, 使其更加物件導向以及語意化 // true 判斷 of() string 是否為 startsWith() string 開頭
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Str;
$converted = Str::of('foo_bar')->studly();
- Answer: Fluent string 語法, 可以 chain 多個 string operations, 使其更加物件導向以及語意化 // FooBar 將 of() string 轉為 studly case
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Str;
$string = Str::of('Laravel Framework')->substr(8);
$string = Str::of('Laravel Framework')->substr(8, 5);
- Answer: Fluent string 語法, 可以 chain 多個 string operations, 使其更加物件導向以及語意化 // Framework // Frame 從 of() string 中, 根據 substr() 中指定的起始點與 length, return 該 string
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Str;
$string = Str::of('Laravel')
->append(' Framework')
->tap(function ($string) {
dump('String after append: ' . $string);
})
->upper();
- Answer: Fluent string 語法, 可以 chain 多個 string operations, 使其更加物件導向以及語意化 // LARAVEL FRAMEWORK 可以對 of() string 做一些邏輯處理, 但不更動到 of() string, tap() 會 return 原本的 of() string
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Str;
$converted = Str::of('a nice title uses the correct case')->title();
- Answer: Fluent string 語法, 可以 chain 多個 string operations, 使其更加物件導向以及語意化 // A Nice Title Uses The Correct Case 將 of() string 中的每一個 word 第一個字轉大寫
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Str;
$string = Str::of(' Laravel ')->trim();
$string = Str::of('/Laravel/')->trim('/');
- Answer: Fluent string 語法, 可以 chain 多個 string operations, 使其更加物件導向以及語意化 // ‘Laravel’ // ‘Laravel’ 從 of() string 中, trim 指定的 string
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Str;
$string = Str::of('foo bar')->ucfirst();
- Answer: Fluent string 語法, 可以 chain 多個 string operations, 使其更加物件導向以及語意化 // Foo bar 將 of() string 第一個 word 第一個字轉大寫
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Str;
$adjusted = Str::of('laravel')->upper();
- Answer: Fluent string 語法, 可以 chain 多個 string operations, 使其更加物件導向以及語意化 // LARAVEL 將 of() string 轉大寫
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Str;
$string = Str::of('Taylor')
->when(true, function ($string) {
return $string->append(' Otwell');
});
// 'Taylor Otwell'
- Answer: Fluent string 語法, 可以 chain 多個 string operations, 使其更加物件導向以及語意化 當 when() arg1 為 true 時, 執行 arg2 closure, closure 可帶入 of() string, 也可定義 arg3, 當 arg1 為 false 時要執行的 closure
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Str;
$string = Str::of(' ')->whenEmpty(function ($string) {
return $string->trim()->prepend('Laravel');
});
- Answer: Fluent string 語法, 可以 chain 多個 string operations, 使其更加物件導向以及語意化 // ‘Laravel’ 如果 of() string 為 empty, 則 invoke whenEmpty() closure
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Str;
$string = Str::of('Perfectly balanced, as all things should be.')->words(3, ' >>>');
- Answer: Fluent string 語法, 可以 chain 多個 string operations, 使其更加物件導向以及語意化 // Perfectly balanced, as >>> 從 of() string 中取 3 個 words, 剩下的截掉, 並顯示
>>>
# URLs
以下的 Laravel example code 的意思是?
- Example:
<?php
use App\Http\Controllers\HomeController;
$url = action([UserController::class, 'profile'], ['id' => 1]);
- Answer: 產生該指定 controller 的 url, 但前提是該 controller 已有對應的 route, action() arg2 可帶入 route url parameter
以下的 Laravel example code 的意思是?
- Example:
<?php
// ASSET_URL=http://example.com/assets
$url = asset('img/photo.jpg'); // http://example.com/assets/img/photo.jpg
- Answer: 產生一個對應指定資源的 url, domain 會隨著 .env 的 ASSET_URL 而改變, 如果有使用 S3 或 CDN, 可在此設定
以下的 Laravel example code 的意思是?
- Example:
<?php
$url = route('route.name', ['id' => 1], false);
- Answer: route() 可產生一個指定 named route 的 url, arg2 可帶入該 route 會用到的 url parameter 預設產生 absolute url, 若要產生 relative, 可在 arg3 帶入 false
以下的 Laravel example code 的意思是?
- Example:
<?php
$url = secure_asset('img/photo.jpg');
- Answer: 產生一個對應指定資源的 https url, domain 會隨著 .env 的 ASSET_URL 而改變, 如果有使用 S3 或 CDN, 可在此設定
以下的 Laravel example code 的意思是?
- Example:
<?php
$url = secure_url('user/profile');
$url = secure_url('user/profile', [1]);
- Answer: 產生一個導向 named route 的 HTTPS url, 可在 arg2 帶入 url parameter
以下的 Laravel example code 的意思是?
- Example:
<?php
$url = url('user/profile');
$url = url('user/profile', [1]);
- Answer: 產生一個導向 named route 的 HTTP url, 可在 arg2 帶入 url parameter
以下的 Laravel example code 的意思是?
- Example:
<?php
$current = url()->current();
$full = url()->full();
$previous = url()->previous();
- Answer: 如果 url() 沒有帶入 parameter, 那就會回傳一個 UrlGenerator instance, 可使用相關的 method
# Miscellaneous
以下的 Laravel example code 的意思是?
- Example:
<?php
abort(403);
- Answer: 直接 throw 一個 HTTP exception, 會由 exception handler 渲染輸出
以下的 Laravel example code 的意思是?
- Example:
<?php
abort(403, 'Unauthorized.', $headers);
- Answer: throw 一個 HTTP exception, 且定義 message 以及其 header
以下的 Laravel example code 的意思是?
- Example:
<?php
abort_if(! Auth::user()->isAdmin(), 403);
- Answer: 如果 arg1 為 true, 則 throw 一個 HTTP exception
以下的 Laravel example code 的意思是?
- Example:
<?php
abort_unless(Auth::user()->isAdmin(), 403);
- Answer: 如果 arg1 不為 true, 則 throw 一個 HTTP exception
以下的 Laravel example code 的意思是?
- Example:
<?php
$container = app();
- Answer: return 一個 service container instance
以下的 Laravel example code 的意思是?
- Example:
<?php
$api = app('HelpSpot\API');
- Answer: 可 pass class 或 instance name, 可從 container 中 resolve 該 class 或 instance
以下的 Laravel example code 的意思是?
- Example:
<?php
$user = auth()->user();
- Answer: auth() return 一個 authenticator instance, 也可使用
Auth
facade
以下的 Laravel example code 的意思是?
- Example:
<?php
$user = auth('admin')->user();
- Answer: auth() return 一個 authenticator instance, 也可使用
Auth
facade, 也可指定要使用哪一個 guard
以下的 Laravel example code 的意思是?
- Example:
<?php
return back($status = 302, $headers = [], $fallback = '/');
return back();
- Answer: 產生一個 redirect HTTP response, 回到上一個 location
以下的 Laravel example code 的意思是?
- Example:
<?php
$password = bcrypt('my-secret-password');
- Answer: 使用 Bcrypt hash value, 相當於 Hash::make()
以下的 Laravel example code 的意思是?
- Example:
<?php
blank('');
blank(' ');
blank(null);
blank(collect());
blank(0);
blank(true);
blank(false);
- Answer: // true // false 判斷 given value 是否為 blank, 相反的 method 為
filled()
以下的 Laravel example code 的意思是?
- Example:
<?php
broadcast(new UserRegistered($user));
broadcast(new UserRegistered($user))->toOthers();
- Answer: 將指定 event 廣播給其 listeners
以下的 Laravel example code 的意思是?
- Example:
<?php
$value = cache('key');
$value = cache('key', 'default');
- Answer: 從 default cache 取得 given key 的 value, 若 given key 不存在, 則取 default
以下的 Laravel example code 的意思是?
- Example:
<?php
cache(['key' => 'value'], 300);
cache(['key' => 'value'], now()->addSeconds(10));
- Answer: 當 pass array 到 cache() 時, 為存值到 cache 中, 可在 arg2 定義有效期限
以下的 Laravel example code 的意思是?
- Example:
<?php
$traits = class_uses_recursive(App\Models\User::class);
- Answer: 回傳所有被該 class 以及其 parent class 使用的 traits
以下的 Laravel example code 的意思是?
- Example:
<?php
$collection = collect(['taylor', 'abigail']);
- Answer: 將 given value 轉為 collection
以下的 Laravel example code 的意思是?
- Example:
<?php
$value = config('app.timezone');
$value = config('app.timezone', $default);
- Answer: 取得 config dir 中, 指定的檔案中的 key value, 若無則取得 default
以下的 Laravel example code 的意思是?
- Example:
<?php
config(['app.debug' => true]);
- Answer: 在 run time 改變 config 的值, 只在該次 request 中有效
以下的 Laravel example code 的意思是?
- Example:
<?php
$cookie = cookie('name', 'value', $minutes);
- Answer: 建立指定 cookie with key & value, 以及有效期限
以下的 Laravel example code 的意思是?
- Example:
<?php
{{ csrf_field() }}
- Answer: 在 form 中產生 csrf token, 以避免 csrf attack
以下的 Laravel example code 的意思是?
- Example:
<?php
$token = csrf_token();
- Answer: 取得當次 request 的 CSRF token
以下的 Laravel example code 的意思是?
- Example:
<?php
dd($value);
dd($value1, $value2, $value3, ...);
- Answer: dump and die
以下的 Laravel example code 的意思是?
- Example:
<?php
dispatch(new App\Jobs\SendEmails);
- Answer: dispatch 一個 job, 如果該 job implement ShouldQueue, 則走 queue
以下的 Laravel example code 的意思是?
- Example:
<?php
$result = dispatch_now(new App\Jobs\SendEmails);
- Answer: 立即 dispatch 一個 job, 不走 queue
以下的 Laravel example code 的意思是?
- Example:
<?php
dump($value);
dump($value1, $value2, $value3, ...);
- Answer: dump given variable
以下的 Laravel example code 的意思是?
- Example:
<?php
$env = env('APP_ENV');
$env = env('APP_ENV', 'production');
- Answer: 取得 .env 值, 當 config:cache 時, 此 function 失效
以下的 Laravel example code 的意思是?
- Example:
<?php
event(new UserRegistered($user));
- Answer: dispatch 指定的 event
以下的 Laravel example code 的意思是?
- Example:
<?php
filled(0);
filled(true);
filled(false);
// true
filled('');
filled(' ');
filled(null);
filled(collect());
// false
- Answer: 判斷 given value 是否 filled, 相反的 function 為 blank()
以下的 Laravel example code 的意思是?
- Example:
<?php
info('User login attempt failed.', ['id' => $user->id]);
- Answer: 會將 given value 寫到 log, 也可帶入 array
以下的 Laravel example code 的意思是?
- Example:
<?php
logger('User login attempt failed.', ['id' => $user->id]);
- Answer: 將 given value 寫到 debug level log
以下的 Laravel example code 的意思是?
- Example:
<?php
logger()->error('You are not allowed here.');
- Answer: 當 logger() 沒有 given value 時, 將回傳 logger instance
以下的 Laravel example code 的意思是?
- Example:
<?php
<form method="POST">
{{ method_field('DELETE') }}
</form>
- Answer: method_field 產生一個 HTML
hidden
input field, 包含著 HTTP verb, 用在 HTTP FORM 不支援的 HTTP method
以下的 Laravel example code 的意思是?
- Example:
<?php
$now = now();
- Answer: now() 建立一個
Illuminate\Support\Carbon
以下的 Laravel example code 的意思是?
- Example:
<?php
$value = old('value');
$value = old('value', 'default');
- Answer: 取得 session old value
以下的 Laravel example code 的意思是?
- Example:
<?php
return optional($user->address)->street;
{!! old('name', optional($user)->name) !!}
- Answer: optional 接受任何形式的 argument, 如果 given object 為 null, 當呼叫該 object 的 method 或 property 時會 return null, 則不會 throw error 如果 $user 本身就有可能是 null 或非 object, 那就要把 $user 也放到另外一個 optional 才行
以下的 Laravel example code 的意思是?
- Example:
<?php
return optional(User::find($id), function ($user) {
return $user->name;
});
- Answer: optional() 可帶入 second args closure, 當 arg1 非為 null 時, 會觸發 arg2 closure
以下的 Laravel example code 的意思是?
- Example:
<?php
$policy = policy(App\Models\User::class);
- Answer: 取得 policy instance
以下的 Laravel example code 的意思是?
- Example:
<?php
return redirect($to = null, $status = 302, $headers = [], $https = null);
return redirect('/home');
return redirect()->route('route.name');
- Answer: 當有 pass args 時, return redirect HTTP response, 否則 return redirector instance
以下的 Laravel example code 的意思是?
- Example:
<?php
report($e);
- Answer: 使用 default exception handler report exception
以下的 Laravel example code 的意思是?
- Example:
<?php
report('Something went wrong.');
- Answer: 當 pass string 時, 會建立一個 exception, with the given string
以下的 Laravel example code 的意思是?
- Example:
<?php
$request = request();
$value = request('key', $default);
- Answer: 回傳 current Request instance
以下的 Laravel example code 的意思是?
- Example:
<?php
return rescue(function () {
return $this->method();
});
- Answer: 會執行一個 closure, 如果有 error, 會 report error 並繼續往下執行
以下的 Laravel example code 的意思是?
- Example:
<?php
return rescue(function () {
return $this->method();
}, false);
return rescue(function () {
return $this->method();
}, function () {
return $this->failure();
});
- Answer: 也可帶入 rescue() second args, 當 arg1 closure throw error 時, return second args
以下的 Laravel example code 的意思是?
- Example:
<?php
$api = resolve('HelpSpot\API');
- Answer: 使用 service container resolve given class
以下的 Laravel example code 的意思是?
- Example:
<?php
return response('Hello World', 200, $headers);
return response()->json(['foo' => 'bar'], 200, $headers);
- Answer: 如果有 pass args, 則產生一個 response instance, 否則則取得 response factory instance
以下的 Laravel example code 的意思是?
- Example:
<?php
return retry(5, function () {
// Attempt 5 times while resting 100ms in between attempts...
}, 100);
- Answer: 嘗試執行 given closure, 如果 throw error, 則重試, 直到達到 arg1 的次數, 若沒有 error, 則 return value, 有且達到次數上限則 throw error
以下的 Laravel example code 的意思是?
- Example:
<?php
$value = session('key');
- Answer: 取得 session value
以下的 Laravel example code 的意思是?
- Example:
<?php
session(['chairs' => 7, 'instruments' => 3]);
- Answer: set session key / value
以下的 Laravel example code 的意思是?
- Example:
<?php
$value = session()->get('key');
session()->put('key', $value);
- Answer: 如果沒有 pass args, session() 回傳 session store instance
以下的 Laravel example code 的意思是?
- Example:
<?php
$user = tap($user)->update([
'name' => $name,
'email' => $email,
]);
- Answer: tap() 可 pass 進一個 model, 並執行該 model 的 method, 執行後, $user 還是 pass 進去之前的 $user, 不會變成 update() 之後的 $user
以下的 Laravel example code 的意思是?
- Example:
<?php
$user = tap(User::first(), function ($user) {
$user->name = 'taylor';
$user->save();
});
- Answer: tap() 可 pass two args, 並將 arg1 pass 進 arg2 closure, 但 tap() return 的 value 為 pass 進去之前的 value, 不受 arg2 closure 影響
以下的 Laravel example code 的意思是?
- Example:
<?php
return $user->tap(function ($user) {
//
});
- Answer: 可在 model user Tappable trait, 然後就可使用 tap method
以下的 Laravel example code 的意思是?
- Example:
<?php
throw_if(! Auth::user()->isAdmin(), AuthorizationException::class);
throw_if(
! Auth::user()->isAdmin(),
AuthorizationException::class,
'You are not allowed to access this page.'
);
- Answer: 如果 arg1 為 true, 則 throw arg2 exception
以下的 Laravel example code 的意思是?
- Example:
<?php
throw_unless(Auth::user()->isAdmin(), AuthorizationException::class);
throw_unless(
Auth::user()->isAdmin(),
AuthorizationException::class,
'You are not allowed to access this page.'
);
- Answer: 如果 arg2 非為 true, 一律 throw arg2 exception
以下的 Laravel example code 的意思是?
- Example:
<?php
$today = today();
- Answer: 取得
Illuminate\Support\Carbon
instance for current date
以下的 Laravel example code 的意思是?
- Example:
<?php
$traits = trait_uses_recursive(\Illuminate\Notifications\Notifiable::class);
- Answer: 取得一個 trait 使用的所有 trait
以下的 Laravel example code 的意思是?
- Example:
<?php
$callback = function ($value) {
return $value * 2;
};
$result = transform(5, $callback);
- Answer: // 10 會將 pass 進去的 arg1 value 帶入 arg2 closure 執行, 如果 arg2 closure return 值非為 blank, 則回傳該值 與 tap 的差異, tap 回傳 pass 進的值, transform 回傳 closure return 值
以下的 Laravel example code 的意思是?
- Example:
<?php
$result = transform(null, $callback, 'The value is blank');
- Answer: // The value is blank 可 pass arg3 為 default 值, 當 arg2 closure 為 null, return arg3 與 with() 類似, 但 transform() 可 pass arg3 as default value 與 tap() 類似, 但 transform() return arg2 closure return value, tap() 回傳 pass 進的 arg1 value
以下的 Laravel example code 的意思是?
- Example:
<?php
$validator = validator($data, $rules, $messages);
- Answer: validator function 建立一個 new validator instance, 與 Validator facade 效果相同
以下的 Laravel example code 的意思是?
- Example:
<?php
$result = value(true);
$result = value(function () {
return false;
});
- Answer: // true // false return given value, 如果給 closure, 會回傳 closure return value
以下的 Laravel example code 的意思是?
- Example:
<?php
return view('auth.login');
- Answer: 取得一個 new view instance
以下的 Laravel example code 的意思是?
- Example:
<?php
$callback = function ($value) {
return (is_numeric($value)) ? $value * 2 : 0;
};
$result = with(5, $callback);
$result = with(null, $callback);
$result = with(5, null);
- Answer: // 10 // 0 // 5 會將 arg1 帶入 arg2 closure, 如果 closure return null, 則 return arg1, 否則則 return closure return value 與 transform 類似, 但 transform 可 pass arg3 as default value