• Laravel 的辅助函数列表
    • 简介
    • 可用方法
      • 数组 & 对象
      • 路径
      • 字符串
      • URLs
      • 其他
    • 方法列表
    • 数组
      • array_add() {#collection-method .first-collection-method}
      • array_collapse() {#collection-method}
      • array_divide() {#collection-method}
      • array_dot() {#collection-method}
      • array_except() {#collection-method}
      • array_first() {#collection-method}
      • array_flatten() {#collection-method}
      • array_forget() {#collection-method}
      • array_get() {#collection-method}
      • array_has() {#collection-method}
      • array_last() {#collection-method}
      • array_only() {#collection-method}
      • array_pluck() {#collection-method}
      • array_prepend() {#collection-method}
      • array_pull() {#collection-method}
      • array_random() {#collection-method}
      • array_set() {#collection-method}
      • array_sort() {#collection-method}
      • array_sort_recursive() {#collection-method}
      • array_where() {#collection-method}
      • array_wrap() {#collection-method}
      • data_fill() {#collection-method}
      • data_get() {#collection-method}
      • data_set() {#collection-method}
      • head() {#collection-method}
      • last() {#collection-method}
  • 路径
    • app_path() {#collection-method}
    • base_path() {#collection-method}
    • config_path() {#collection-method}
    • database_path() {#collection-method}
    • mix() {#collection-method}
    • public_path() {#collection-method}
    • resource_path() {#collection-method}
    • storage_path() {#collection-method}
  • 字符串
    • __() {#collection-method}
    • camel_case() {#collection-method}
    • class_basename() {#collection-method}
    • e() {#collection-method}
    • ends_with() {#collection-method}
    • kebab_case() {#collection-method}
    • preg_replace_array()
    • snake_case() {#collection-method}
    • starts_with() {#collection-method}
    • str_after() {#collection-method}
    • str_before() {#collection-method}
    • str_contains() {#collection-method}
    • str_finish() {#collection-method}
    • str_is() {#collection-method}
    • str_limit() {#collection-method}
    • str_plural() {#collection-method}
    • str_random() {#collection-method}
    • str_replace_array() {#collection-method}
    • str_replace_first() {#collection-method}
    • str_replace_last() {#collection-method}
    • str_singular() {#collection-method}
    • str_slug() {#collection-method}
    • str_start() {#collection-method}
    • studly_case() {#collection-method}
    • title_case() {#collection-method}
    • trans() {#collection-method}
    • trans_choice() {#collection-method}
  • URLs
    • action() {#collection-method}
    • asset() {#collection-method}
    • secure_asset() {#collection-method}
    • route() {#collection-method}
    • secure_url() {#collection-method}
    • url() {#collection-method}
  • 其他
    • abort() {#collection-method}
    • abort_if() {#collection-method}
    • abort_unless() {#collection-method}
    • app() {#collection-method}
    • auth() {#collection-method}
    • back() {#collection-method}
    • bcrypt() {#collection-method}
    • broadcast() {#collection-method}
    • blank() {#collection-method}
    • cache() {#collection-method}
    • class_uses_recursive() {#collection-method}
    • collect() {#collection-method}
    • config() {#collection-method}
    • cookie() {#collection-method}
    • csrf_field() {#collection-method}
    • csrf_token() {#collection-method}
    • dd() {#collection-method}
    • decrypt() {#collection-method}
    • dispatch() {#collection-method}
    • dispatch_now() {#collection-method}
    • dump() {#collection-method}
    • encrypt() {#collection-method}
    • env() {#collection-method}
    • event() {#collection-method}
    • factory() {#collection-method}
    • filled() {#collection-method}
    • info() {#collection-method}
    • logger() {#collection-method}
    • method_field() {#collection-method}
    • now() {#collection-method}
    • old() {#collection-method}
    • optional() {#collection-method}
    • policy() {#collection-method}
    • redirect() {#collection-method}
    • report() {#collection-method}
    • request() {#collection-method}
    • rescue() {#collection-method}
    • resolve() {#collection-method}
    • response() {#collection-method}
    • retry() {#collection-method}
    • session() {#collection-method}
    • tap() {#collection-method}
    • today() {#collection-method}
    • throw_if() {#collection-method}
    • throw_unless() {#collection-method}
    • trait_uses_recursive() {#collection-method}
    • transform() {#collection-method}
    • validator() {#collection-method}
    • value() {#collection-method}
    • view() {#collection-method}
    • with() {#collection-method}
  • 译者署名

    Laravel 的辅助函数列表

    • 简介
    • 可用方法

    简介

    Laravel 包含各种各样的全局「辅助」PHP 函数,框架本身也大量地使用了这些功能;如果你觉得方便,你可以在你的应用中自由的使用它们。

    可用方法

    数组 & 对象



    array_add
    array_collapse
    array_divide
    array_dot
    array_except
    array_first
    array_flatten
    array_forget
    array_get
    array_has
    array_last
    array_only
    array_pluck
    array_prepend
    array_pull
    array_random
    array_set
    array_sort
    array_sort_recursive
    array_where
    array_wrap
    data_fill
    data_get
    data_set
    head
    last

    路径



    app_path
    base_path
    config_path
    database_path
    mix
    public_path
    resource_path
    storage_path

    字符串



    __
    camel_case
    class_basename
    e
    ends_with
    kebab_case
    preg_replace_array
    snake_case
    starts_with
    str_after
    str_before
    str_contains
    str_finish
    str_is
    str_limit
    str_plural
    str_random
    str_replace_array
    str_replace_first
    str_replace_last
    str_singular
    str_slug
    str_start
    studly_case
    title_case
    trans
    trans_choice

    URLs



    action
    asset
    secure_asset
    route
    secure_url
    url

    其他



    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

    方法列表

    数组

    array_add() {#collection-method .first-collection-method}

    如果给定的健不在数组中,那么 array_add 函数将会把给定的键/值对添加到数组中:

    1. $array = array_add(['name' => 'Desk'], 'price', 100);
    2. // ['name' => 'Desk', 'price' => 100]

    array_collapse() {#collection-method}

    array_collapse 函数将多个单数组合并成一个数组:

    1. $array = array_collapse([[1, 2, 3], [4, 5, 6], [7, 8, 9]]);
    2. // [1, 2, 3, 4, 5, 6, 7, 8, 9]

    array_divide() {#collection-method}

    array_divide 函数返回两个数组,一个包含原始数组的健,另一个包含原始数组的值:

    1. list($keys, $values) = array_divide(['name' => 'Desk']);
    2. // $keys: ['name']
    3. // $values: ['Desk']

    array_dot() {#collection-method}

    array_dot 函数将多维数组平铺到一维数组中,该数组使用「点」符号表示深度:

    1. $array = array_dot(['foo' => ['bar' => 'baz']]);
    2. // ['foo.bar' => 'baz'];

    array_except() {#collection-method}

    array_except 函数从数组中删除给定的键/值对:

    1. $array = ['name' => 'Desk', 'price' => 100];
    2. $array = array_except($array, ['price']);
    3. // ['name' => 'Desk']

    array_first() {#collection-method}

    array_first 函数返回数组中第一个通过指定测试的元素:

    1. $array = [100, 200, 300];
    2. $value = array_first($array, function ($value, $key) {
    3. return $value >= 150;
    4. });
    5. // 200

    将默认值作为第三个参数传递给该方法。如果没有值通过测试,则返回该值:

    1. $value = array_first($array, $callback, $default);

    array_flatten() {#collection-method}

    array_flatten 函数将多维数组平铺为一维数组。

    1. $array = ['name' => 'Joe', 'languages' => ['PHP', 'Ruby']];
    2. $array = array_flatten($array);
    3. // ['Joe', 'PHP', 'Ruby'];

    array_forget() {#collection-method}

    array_forget 函数使用「点」符号从深度嵌套数组中移除给定的键/值对:

    1. $array = ['products' => ['desk' => ['price' => 100]]];
    2. array_forget($array, 'products.desk');
    3. // ['products' => []]

    array_get() {#collection-method}

    array_get 函数使用「点」符号从深度嵌套的数组中检索值:

    1. $array = ['products' => ['desk' => ['price' => 100]]];
    2. $value = array_get($array, 'products.desk');
    3. // ['price' => 100]

    array_get 函数也接受一个默认值,如果没有找到指定的健,则返回该值:

    1. $value = array_get($array, 'names.john', 'default');

    array_has() {#collection-method}

    array_has 函数使用「点」符号检查数组中是否存在给定的项目或项目组:

    1. $array = ['product' => ['name' => 'desk', 'price' => 100]];
    2. $hasItem = array_has($array, 'product.name');
    3. // true
    4. $hasItems = array_has($array, ['product.price', 'product.discount']);
    5. // false

    array_last() {#collection-method}

    array_last 函数返回数组中最后一个通过指定测试的元素:

    1. $array = [100, 200, 300, 110];
    2. $value = array_last($array, function ($value, $key) {
    3. return $value >= 150;
    4. });
    5. // 300

    将默认值作为第三个参数传递给该方法。如果没有值通过测试,则返回该值:

    1. $last = array_last($array, $callback, $default);

    array_only() {#collection-method}

    array_only 函数仅返回给定数组中指定的键/值对:

    1. $array = ['name' => 'Desk', 'price' => 100, 'orders' => 10];
    2. $array = array_only($array, ['name', 'price']);
    3. // ['name' => 'Desk', 'price' => 100]

    array_pluck() {#collection-method}

    array_pluck 函数从数组中检索给定键的所有值:

    1. $array = [
    2. ['developer' => ['id' => 1, 'name' => 'Taylor']],
    3. ['developer' => ['id' => 2, 'name' => 'Abigail']],
    4. ];
    5. $array = array_pluck($array, 'developer.name');
    6. // ['Taylor', 'Abigail'];

    你也可以指定生成的列表的键:

    1. $array = array_pluck($array, 'developer.name', 'developer.id');
    2. // [1 => 'Taylor', 2 => 'Abigail'];

    array_prepend() {#collection-method}

    array_prepend 函数将一个项目推到数组的开头:

    1. $array = ['one', 'two', 'three', 'four'];
    2. $array = array_prepend($array, 'zero');
    3. // $array: ['zero', 'one', 'two', 'three', 'four']

    你可以指定用于该值的键:

    1. $array = ['price' => 100];
    2. $array = array_prepend($array, 'Desk', 'name');
    3. // ['name' => 'Desk', 'price' => 100]

    array_pull() {#collection-method}

    array_pull 函数返回并从数组中删除键/值对:

    1. $array = ['name' => 'Desk', 'price' => 100];
    2. $name = array_pull($array, 'name');
    3. // $name: Desk
    4. // $array: ['price' => 100]

    将默认值作为第三个参数传递给该方法。如果键不存在,则返回该值:

    1. $value = array_pull($array, $key, $default);

    array_random() {#collection-method}

    array_random 函数从数组中返回一个随机值:

    1. $array = [1, 2, 3, 4, 5];
    2. $random = array_random($array);
    3. // 4 - (随机获取)

    你也可以指定要返回的随机数的数量作为第二个可选参数。一旦你指定了第二个参数,即使数量为 1,这个函数也会返回一个数组:

    1. $items = array_random($array, 2);
    2. // [2, 5] - (随机获取)

    array_set() {#collection-method}

    array_set 函数使用「点」符号在深度嵌套的数组中设置一个值:

    1. $array = ['products' => ['desk' => ['price' => 100]]];
    2. array_set($array, 'products.desk.price', 200);
    3. // ['products' => ['desk' => ['price' => 200]]]

    array_sort() {#collection-method}

    array_sort 函数按照其值排序数组:

    1. $array = ['Desk', 'Table', 'Chair'];
    2. $sorted = array_sort($array);
    3. // ['Chair', 'Desk', 'Table']

    你也可以按给定的闭包返回的结果对数组进行排序:

    1. $array = [
    2. ['name' => 'Desk'],
    3. ['name' => 'Table'],
    4. ['name' => 'Chair'],
    5. ];
    6. $sorted = array_values(array_sort($array, function ($value) {
    7. return $value['name'];
    8. }));
    9. /*
    10. [
    11. ['name' => 'Chair'],
    12. ['name' => 'Desk'],
    13. ['name' => 'Table'],
    14. ]
    15. */

    array_sort_recursive() {#collection-method}

    array_sort_recursive 函数使用 sort 函数递归排序数组:

    1. $array = [
    2. ['Roman', 'Taylor', 'Li'],
    3. ['PHP', 'Ruby', 'JavaScript'],
    4. ];
    5. $sorted = array_sort_recursive($array);
    6. /*
    7. [
    8. ['Li', 'Roman', 'Taylor'],
    9. ['JavaScript', 'PHP', 'Ruby'],
    10. ]
    11. */

    array_where() {#collection-method}

    array_where 函数使用给定的闭包来过滤数组:

    1. $array = [100, '200', 300, '400', 500];
    2. $array = array_where($array, function ($value, $key) {
    3. return is_string($value);
    4. });
    5. // [1 => 200, 3 => 400]

    array_wrap() {#collection-method}

    array_wrap 函数将给定的值包装成一个数组。如果给定的值已经是一个数组,则不会被改变:

    1. $string = 'Laravel';
    2. $array = array_wrap($string);
    3. // [0 => 'Laravel']

    data_fill() {#collection-method}

    data_fill 函数使用「点」符号在嵌套数组或对象内设置缺少的值:

    1. $data = ['products' => ['desk' => ['price' => 100]]];
    2. data_fill($data, 'products.desk.price', 200);
    3. // ['products' => ['desk' => ['price' => 100]]]
    4. data_fill($data, 'products.desk.discount', 10);
    5. // ['products' => ['desk' => ['price' => 100, 'discount' => 10]]]

    该函数也接受星号「*」作为通配符,并相应地填写目标:

    1. $data = [
    2. 'products' => [
    3. ['name' => 'Desk 1', 'price' => 100],
    4. ['name' => 'Desk 2'],
    5. ],
    6. ];
    7. data_fill($data, 'products.*.price', 200);
    8. /*
    9. [
    10. 'products' => [
    11. ['name' => 'Desk 1', 'price' => 100],
    12. ['name' => 'Desk 2', 'price' => 200],
    13. ],
    14. ]
    15. */

    data_get() {#collection-method}

    data_get 函数使用「点」符号从嵌套数组或对象中检索值:

    1. $data = ['products' => ['desk' => ['price' => 100]]];
    2. $price = data_get($data, 'products.desk.price');
    3. // 100

    data_get 函数还接受默认值作为第三个参数,如果找不到指定的键,将返回该值:

    1. $discount = data_get($data, 'products.desk.discount', 0);
    2. // 0

    data_set() {#collection-method}

    data_set 函数使用「点」符号在嵌套数组或对象内设置一个值:

    1. $data = ['products' => ['desk' => ['price' => 100]]];
    2. data_set($data, 'products.desk.price', 200);
    3. // ['products' => ['desk' => ['price' => 200]]]

    这个函数也接受通配符「*」,并相应地在目标上设置值:

    1. $data = [
    2. 'products' => [
    3. ['name' => 'Desk 1', 'price' => 100],
    4. ['name' => 'Desk 2', 'price' => 150],
    5. ],
    6. ];
    7. data_set($data, 'products.*.price', 200);
    8. /*
    9. [
    10. 'products' => [
    11. ['name' => 'Desk 1', 'price' => 200],
    12. ['name' => 'Desk 2', 'price' => 200],
    13. ],
    14. ]
    15. */

    默认情况下,所有现有的值都会被覆盖。如果你只想设置一个不存在值,你可以传递 false 作为第三个参数:

    1. $data = ['products' => ['desk' => ['price' => 100]]];
    2. data_set($data, 'products.desk.price', 200, false);
    3. // ['products' => ['desk' => ['price' => 100]]]

    head() {#collection-method}

    head 函数返回给定数组中的第一个元素:

    1. $array = [100, 200, 300];
    2. $first = head($array);
    3. // 100

    last() {#collection-method}

    last 函数返回给定数组中的最后一个元素:

    1. $array = [100, 200, 300];
    2. $last = last($array);
    3. // 300

    路径

    app_path() {#collection-method}

    app_path 返回 app 目录的完整路径。你还可以使用 app_path 函数来生成相对于 app 目录的文件完整路径:

    1. $path = app_path();
    2. $path = app_path('Http/Controllers/Controller.php');

    base_path() {#collection-method}

    base_path 函数返回项目根目录的完整路径。你还可以使用 base_path 函数生成指定文件相对于项目根目录的完整路径:

    1. $path = base_path();
    2. $path = base_path('vendor/bin');

    config_path() {#collection-method}

    config_path 函数返回应用程序配置目录的完整路径。你也可以使用 config_path 函数来生成应用程序配置目录中给定文件的完整路径:

    1. $path = config_path();

    database_path() {#collection-method}

    database_path 函数返回应用程序数据库目录的完整路径。你也可以使用 database_path 函数来生成数据库目录中给定文件的完整路径:

    1. $path = database_path();

    mix() {#collection-method}

    mix 函数获取 版本化 Mix 文件 的路径:

    1. mix($file);

    public_path() {#collection-method}

    public_path 函数返回 public 目录的完整路径。你也可以使用 public_path 函数来生成 public 目录中给定文件的完整路径:

    1. $path = public_path();

    resource_path() {#collection-method}

    resource_path 函数返回 resources 目录的完整路径。你也可以使用 resource_path 函数来生成相对于资源目录的指定文件的完整路径:

    1. $path = resource_path();
    2. $path = resource_path('assets/sass/app.scss');

    storage_path() {#collection-method}

    storage_path 函数返回 storage 目录的完整路径。你也可以使用 storage_path 来生成相对于储存目录的指定文件的完整路径:

    1. $path = storage_path();
    2. $path = storage_path('app/file.txt');

    字符串

    __() {#collection-method}

    __ 函数使用你的 本地化文件 来翻译给定的翻译字符串或翻译键:

    1. echo __('Welcome to our application');
    2. echo __('messages.welcome');

    如果指定的翻译字符串或键不存在,则 __ 函数会简单地返回给定的值。所以,按照上面的例子,如果翻译键 messages.welcome 不存在,__ 方法会将其直接返回。

    camel_case() {#collection-method}

    camel_case 函数将给定的值符传转换为「驼峰命名」:

    1. $camel = camel_case('foo_bar');
    2. // fooBar

    class_basename() {#collection-method}

    class_basename 返回给定类删除命名空间的类名:

    1. $class = class_basename('Foo\Bar\Baz');
    2. // Baz

    e() {#collection-method}

    e 函数将 double_encode 选项设置为 false 来运行 PHP 的 htmlspecialchars 函数:

    1. echo e('<html>foo</html>');
    2. // &lt;html&gt;foo&lt;/html&gt;

    ends_with() {#collection-method}

    ends_with 函数判断给定的字符串是否以给定的值结尾:

    1. $value = ends_with('This is my name', 'name');
    2. // true

    kebab_case() {#collection-method}

    kebab_case 函数将给定的字符串转换为「短横线命名」:

    1. $value = kebab_case('fooBar');
    2. // foo-bar

    preg_replace_array()

    preg_replace_array 函数使用数组顺序替换字符串中的给定模式:

    1. $string = '活动将在 :start 和 :end 之间进行';
    2. $replaced = preg_replace_array('/:[a-z_]+/', ['8:30', '9:00'], $string);
    3. // 活动将在 8:30 至 9:00 之间进行

    snake_case() {#collection-method}

    snake_case 函数将给定的字符串转换为「蛇形命名」:

    1. $snake = snake_case('fooBar');
    2. // foo_bar

    starts_with() {#collection-method}

    starts_with 函数判断给定的字符串的开头是否是指定值:

    1. $value = starts_with('This is my name', 'This');
    2. // true

    str_after() {#collection-method}

    str_after 函数返回字符串中指定值之后的所有内容:

    1. $slice = str_after('This is my name', 'This is');
    2. // ' my name'

    str_before() {#collection-method}

    str_before 函数返回字符串中给定值之前的所有内容:

    1. $slice = str_before('This is my name', 'my name');
    2. // 'This is '

    str_contains() {#collection-method}

    str_contains 函数判断给定的字符串是否包含给定的值:

    1. $value = str_contains('This is my name', 'my');
    2. // true

    你也可以传递一个值的数组来判断给定的字符串是否包含任何值:

    1. $value = str_contains('This is my name', ['my', 'foo']);
    2. // true

    str_finish() {#collection-method}

    str_finish 函数将给定字符串以给定值结尾返回(如果它尚未以给定值结尾):

    1. $string = str_finish('this/string', '/');
    2. // this/string/
    3. $string2 = str_finish('this/string/', '/');
    4. // this/string/

    str_is() {#collection-method}

    str_is 函数判断给定的字符串是否匹配给定的模式。星号可以用来表示通配符:

    1. $value = str_is('foo*', 'foobar');
    2. // true
    3. $value = str_is('baz*', 'foobar');
    4. // false

    str_limit() {#collection-method}

    str_limit 函数按给定的长度截断给定的字符串:

    1. $truncated = str_limit('The quick brown fox jumps over the lazy dog', 20);
    2. // The quick brown fox...

    你也可以传递第三个参数来改变将被追加到最后的字符串:

    1. $truncated = str_limit('The quick brown fox jumps over the lazy dog', 20, ' (...)');
    2. // The quick brown fox (...)

    str_plural() {#collection-method}

    str_plural 函数将字符串转换为复数形式。这个函数目前仅支持英文:

    1. $plural = str_plural('car');
    2. // cars
    3. $plural = str_plural('child');
    4. // children

    你可以提供一个整数作为函数的第二个参数来检索字符串的单数或复数形式:

    1. $plural = str_plural('child', 2);
    2. // children
    3. $plural = str_plural('child', 1);
    4. // child

    str_random() {#collection-method}

    str_random 函数生成一个指定长度的随机字符串。这个函数数用 PHP 的 random_bytes 函数:

    1. $string = str_random(40);

    str_replace_array() {#collection-method}

    str_replace_array 函数使用数组顺序替换字符串中的给定值:

    1. $string = 'The event will take place between ? and ?';
    2. $replaced = str_replace_array('?', ['8:30', '9:00'], $string);
    3. // The event will take place between 8:30 and 9:00

    str_replace_first() {#collection-method}

    str_replace_first 函数替换字符串中给定值的第一个匹配项:

    1. $replaced = str_replace_first('the', 'a', 'the quick brown fox jumps over the lazy dog');
    2. // a quick brown fox jumps over the lazy dog

    str_replace_last() {#collection-method}

    str_replace_last 函数替换字符串中最后一次出现的给定值:

    1. $replaced = str_replace_last('the', 'a', 'the quick brown fox jumps over the lazy dog');
    2. // the quick brown fox jumps over a lazy dog

    str_singular() {#collection-method}

    str_singular 函数将字符串转换为单数形式。这个函数目前仅支持英文:

    1. $singular = str_singular('cars');
    2. // car

    str_slug() {#collection-method}

    str_slug 函数根据给定的字符串生成一个 URL 友好的「slug」:

    1. $title = str_slug('Laravel 5 Framework', '-');
    2. // laravel-5-framework

    str_start() {#collection-method}

    str_start 函数将给定值的单个实例添加到字符串(如果它尚未以值开始):

    1. $adjusted = str_start('this/string', '/');
    2. // /this/string
    3. $adjusted = str_start('/this/string/', '/');
    4. // /this/string

    studly_case() {#collection-method}

    studly_case 函数将给定的字符串转换为「变种驼峰命名」:

    1. $value = studly_case('foo_bar');
    2. // FooBar

    title_case() {#collection-method}

    title_case 函数将给定的字符串转换为「首字母大写」:

    1. $title = title_case('a nice title uses the correct case');
    2. // A Nice Title Uses The Correct Case

    trans() {#collection-method}

    trans 函数使用你的 本地化文件 来翻译给定的翻译字符串或翻译键:

    1. echo trans('messages.welcome');

    如果指定的翻译键不存在,则 trans 方法会简单地返回给定的键。所以,就上面的例子而言,如果翻译键不存在, trans 方法会返回 messages.welcome

    trans_choice() {#collection-method}

    trans_choice 函数根据词形变化来翻译给定的翻译键:

    1. echo trans_choice('messages.notifications', $unreadCount);

    如果指定的翻译键不存在,trans_choice 方法会简单地返回给定的键。所以,按照上面的例子,如果翻译键不存在,trans_choice 方法会返回 messages.notifications

    URLs

    action() {#collection-method}

    action 函数为指定的控制器动作生成一个 URL。你不需要传递完整的控制器命名空间。只需要传递相对于 App\Http\Controllers 的命名空间的控制器类名称:

    1. $url = action('HomeController@getIndex');

    如果该方法接受路由参数,则可以将它们作为方法的第二个参数传递:

    1. $url = action('UserController@profile', ['id' => 1]);

    asset() {#collection-method}

    asset 函数使用当前请求的协议( HTTP 或 HTTPS )为资源文件生成 URL:

    1. $url = asset('img/photo.jpg');

    secure_asset() {#collection-method}

    secure_asset 函数使用 HTTPS 协议为资源文件生成 URL:

    1. echo secure_asset('foo/bar.zip');

    route() {#collection-method}

    route 函数为给定的命名路由生成一个 URL:

    1. $url = route('routeName');

    如果路由接受参数,则可以将它们作为方法的第二个参数传递:

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

    默认情况下,route 函数生成的是绝对 URL。如果你想生成一个相对 URL,你可以传递 false 作为第三个参数:

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

    secure_url() {#collection-method}

    secure_url 函数为给定的路径生成一个标准的 HTTPS URL:

    1. echo secure_url('user/profile');
    2. echo secure_url('user/profile', [1]);

    url() {#collection-method}

    url 函数生成给定路径的标准 URL:

    1. echo url('user/profile');
    2. echo url('user/profile', [1]);

    如果没有提供路径,则返回 Illuminate\Routing\UrlGenerator 实例:

    1. echo url()->current();
    2. echo url()->full();
    3. echo url()->previous();

    其他

    abort() {#collection-method}

    abort 函数抛出 异常处理 程序呈现的 HTTP 异常:

    1. abort(401);

    你也可以提供额外的响应文本和自定义响应标头:

    1. abort(403, 'Unauthorized.', $headers);

    abort_if() {#collection-method}

    如果给定的布尔表达式计算结果为 trueabort_if 函数将抛出一个 HTTP 异常:

    1. abort_if(! Auth::user()->isAdmin(), 403);

    abort 方法一样,你也可以提供异常的响应文本作为第三个参数,并提供一个自定义响应头数组作为第四个参数。

    abort_unless() {#collection-method}

    如果给定的布尔表达式计算结果为 falseabort_unless 函数将抛出一个 HTTP 异常:

    1. abort_unless(Auth::user()->isAdmin(), 403);

    abort 方法一样,你也可以提供异常的响应文本作为第三个参数,并提供一个自定义响应头数组作为第四个参数。

    app() {#collection-method}

    app 函数返回 服务容器 实例

    1. $container = app();

    你可以传递一个类或接口名称来从容器中解析它:

    1. $api = app('HelpSpot\API');

    auth() {#collection-method}

    auth 函数返回一个 认证 实例。为了方便起见,你可以使用它来替代 Auth facade:

    1. $user = auth()->user();

    如果需要,你可以指定你想要访问的认证实例:

    1. $user = auth('admin')->user();

    back() {#collection-method}

    back() 函数生成一个 重定向 HTTP 响应 到用户之前的位置:

    1. return back($status = 302, $headers = [], $fallback = false);
    2. return back();

    bcrypt() {#collection-method}

    bcrypt 使用 Bcrypt 对给定的值进行散列。你可以使用它替代 Hash facade:

    1. $password = bcrypt('my-secret-password');

    broadcast() {#collection-method}

    broadcast 函数将 广播 给定的 事件 到它的监听器:

    1. broadcast(new UserRegistered($user));

    blank() {#collection-method}

    blank 函数判断给定的值是否为「空」:

    1. blank('');
    2. blank(' ');
    3. blank(null);
    4. blank(collect());
    5. // true
    6. blank(0);
    7. blank(true);
    8. blank(false);
    9. // false

    要使用与 blank 相反的功能,请看 filled 方法。

    cache() {#collection-method}

    cache 函数可以用来从缓存中获取值。如果缓存中不存在给定的健,则返回一个可选的默认值:

    1. $value = cache('key');
    2. $value = cache('key', 'default');

    你可以通过将一组键/值对传递给函数来将其添加到缓存中。与此同时,你还应该传递有效的分钟数或持续时间作为缓存过期时间:

    1. cache(['key' => 'value'], 5);
    2. cache(['key' => 'value'], Carbon::now()->addSeconds(10));

    class_uses_recursive() {#collection-method}

    class_uses_recursive 函数返回一个类使用的所有 traits,包括任何子类使用的 traits:

    1. $traits = class_uses_recursive(App\User::class);

    collect() {#collection-method}

    collect 函数根据给定的数组创建一个 集合 实例:

    1. $collection = collect(['taylor', 'abigail']);

    config() {#collection-method}

    config 函数获取 配置 变量的值。可以使用「点」语法访问配置值,其中包括文件的名称和希望访问的选项。如果配置选项不存在,则可以指定一个默认值并返回:

    1. $value = config('app.timezone');
    2. $value = config('app.timezone', $default);

    可以在运行时通过传递一组键/值对来设置配置变量:

    1. config(['app.debug' => true]);

    cookie 函数创建一个新的 cookie 实例:

    1. $cookie = cookie('name', 'value', $minutes);

    csrf_field() {#collection-method}

    csrf_field 函数生成包含 CSRF 令牌值的 HTML hidden 表单字段。例如,使用 Blade 语法:

    1. {{ csrf_field() }}

    csrf_token() {#collection-method}

    csrf_token 函数获取当前 CSRF 令牌的值:

    1. $token = csrf_token();

    dd() {#collection-method}

    dd 函数输出给定的值并结束脚本运行:

    1. dd($value);
    2. dd($value1, $value2, $value3, ...);

    如果你不想终止脚本运行,请改用 dump 函数。

    decrypt() {#collection-method}

    decrypt 函数使用 Laravel 的 加密器 来解密给定的值:

    1. $decrypted = decrypt($encrypted_value);

    dispatch() {#collection-method}

    dispatch 函数将给定的 任务 推送到 Laravel 任务列队 中:

    1. dispatch(new App\Jobs\SendEmails);

    dispatch_now() {#collection-method}

    dispatch_now 函数立即运行给定的 任务,并从其 handle 方法返回值:

    1. $result = dispatch_now(new App\Jobs\SendEmails);

    dump() {#collection-method}

    dump 函数打印给定的变量:

    1. dump($value);
    2. dump($value1, $value2, $value3, ...);

    如果要在打印变量后停止执行脚本,请改用 dd 函数。

    encrypt() {#collection-method}

    encrypt 函数使用 Laravel 的 加密器 对给定的值进行加密:

    1. $encrypted = encrypt($unencrypted_value);

    env() {#collection-method}

    env 函数获取 环境变量 的值或者返回默认值:

    1. $env = env('APP_ENV');
    2. // 如果环境变量不存在则返回默认值...
    3. $env = env('APP_ENV', 'production');

    event() {#collection-method}

    event 函数将给定的 事件 分派给它的监听器:

    1. event(new UserRegistered($user));

    factory() {#collection-method}

    factory 函数根据给定的类、名称和数量创建一个模型工厂构建器。可以在 测试 or 数据填充 中使用:

    1. $user = factory(App\User::class)->make();

    filled() {#collection-method}

    filled 函数判断给定的值是否不为「空」:

    1. filled(0);
    2. filled(true);
    3. filled(false);
    4. // true
    5. filled('');
    6. filled(' ');
    7. filled(null);
    8. filled(collect());
    9. // false

    要使用与 filled 相反的功能,请看 blank 方法。

    info() {#collection-method}

    info 函数将信息写入日志:

    1. info('Some helpful information!');

    有前后关系的数组也可以传递给函数:

    1. info('User login attempt failed.', ['id' => $user->id]);

    logger() {#collection-method}

    logger 函数可以将一个 debug 级别的消息写入到 日志 中:

    1. logger('Debug message');

    有前后关系的数组也可以传递给函数:

    1. logger('User has logged in.', ['id' => $user->id]);

    如果没有传值给函数则返回 日志 的实例:

    1. logger()->error('You are not allowed here.');

    method_field() {#collection-method}

    method_field 函数生成一个 HTML hidden 表单字段,其中包含表单的 HTTP 动作的欺骗值。例如,使用 Blade 语法:

    1. <form method="POST">
    2. {{ method_field('DELETE') }}
    3. </form>

    now() {#collection-method}

    now 函数为当前时间创建一个新的 Illuminate\Support\Carbon 实例:

    1. $now = now();

    old() {#collection-method}

    old 函数 获取 会话中闪存的 旧输入 值:

    1. $value = old('value');
    2. $value = old('value', 'default');

    optional() {#collection-method}

    optional 函数可以接受任何参数,并且允许你访问该对象的属性或者调用方法。如果给定的对象是 null , 那么属性和方法会简单地返回 null 而不是产生一个错误:

    1. return optional($user->address)->street;
    2. {!! old('name', optional($user)->name) !!}

    policy() {#collection-method}

    policy 方法为给定的类获取一个 策略 实例:

    1. $policy = policy(App\User::class);

    redirect() {#collection-method}

    redirect 函数返回一个 重定向 HTTP 响应,如果没有没有传入参数,则返回重定向实例:

    1. return redirect('/home');
    2. return redirect()->route('route.name');

    report() {#collection-method}

    report 函数将使用 异常处理程序 的 report 方法抛出异常:

    1. report($e);

    request() {#collection-method}

    request 函数返回当前 请求 实例或者获取输入项:

    1. $request = request();
    2. $value = request('key', $default = null)

    rescue() {#collection-method}

    rescue 函数执行给定的闭包并捕获执行期间发生的任何异常。所有被捕获的异常将被发送到你的 异常处理程序 的 report 方法。要注意的是,该请求将继续处理:

    1. return rescue(function () {
    2. return $this->method();
    3. });

    你也可以将第二个参数传递给 rescue 方法。如果在执行闭包时发生异常,这个参数将是应该返回的默认值:

    1. return rescue(function () {
    2. return $this->method();
    3. }, false);
    4. return rescue(function () {
    5. return $this->method();
    6. }, function () {
    7. return $this->failure();
    8. });

    resolve() {#collection-method}

    resolve 函数使用 服务容器 将给定的类或接口名称解析为其实例:

    1. $api = resolve('HelpSpot\API');

    response() {#collection-method}

    response 函数创建 响应 实例或者获取响应工厂实例:

    1. return response('Hello World', 200, $headers);
    2. return response()->json(['foo' => 'bar'], 200, $headers);

    retry() {#collection-method}

    retry 函数尝试执行给定的回调,直到到达给定的最大尝试次数。如果回调没有抛出异常,则返回值将被返回。如果回调抛出异常,它将自动重试。如果超过最大尝试次数,则会抛出异常:

    1. return retry(5, function () {
    2. // 在 100ms 左右尝试 5 次...
    3. }, 100);

    session() {#collection-method}

    session 函数可以用来获取或者设置 Session 值:

    1. $value = session('key');

    你可以通过将一组键/值对传递给该函数来设置值:

    1. session(['chairs' => 7, 'instruments' => 3]);

    如果没有传递值给函数,则返回 Session 实例:

    1. $value = session()->get('key');
    2. session()->put('key', $value);

    tap() {#collection-method}

    tap 函数接受两个参数:一个任意的 $value 和一个闭包。$value 将被传递给闭包,然后由 tap 函数返回。不需要在闭包中使用 return 返回值。

    1. $user = tap(User::first(), function ($user) {
    2. $user->name = 'taylor';
    3. $user->save();
    4. });

    如果没有闭包被传递给 tap 函数,你可以调用给定 $value 的任何方法。而你调用的方法的返回值始终为 $value ,无论方法在其定义中实际返回的是什么。例如,Eloquent 的 update 方法通常会返回一个整数。但是,我们可以强制通过 tap 函数链式调用 update 方法来返回模型本身:

    1. $user = tap($user)->update([
    2. 'name' => $name,
    3. 'email' => $email,
    4. ]);

    today() {#collection-method}

    today 函数为当前日期创建一个新的 Illuminate\Support\Carbon 实例:

    1. $today = today();

    throw_if() {#collection-method}

    如果给定的布尔表达式计算结果为 truethrow_if 函数抛出给定的异常:

    1. throw_if(! Auth::user()->isAdmin(), AuthorizationException::class);
    2. throw_if(
    3. ! Auth::user()->isAdmin(),
    4. AuthorizationException::class,
    5. 'You are not allowed to access this page'
    6. );

    throw_unless() {#collection-method}

    如果给定的布尔表达式计算结果为false,则 throw_unless函数会抛出给定的异常:

    1. throw_unless(Auth::user()->isAdmin(), AuthorizationException::class);
    2. throw_unless(
    3. Auth::user()->isAdmin(),
    4. AuthorizationException::class,
    5. 'You are not allowed to access this page'
    6. );

    trait_uses_recursive() {#collection-method}

    trait_uses_recursive 函数返回一个类使用的所有 trait:

    1. $traits = trait_uses_recursive(\Illuminate\Notifications\Notifiable::class);

    transform() {#collection-method}

    如果给定的值不为空,那么 transform 函数对给定的值执行闭包并返回其结果:

    1. $callback = function ($value) {
    2. return $value * 2;
    3. };
    4. $result = transform(5, $callback);
    5. // 10

    默认值或闭包也可以作为方法的第三个参数传递。如果给定值为空白,则返回该值:

    1. $result = transform(null, $callback, 'The value is blank');
    2. // The value is blank

    validator() {#collection-method}

    validator 函数用给定的参数创建一个新的 验证器 实例。为方便起见,你可以使用它来代替 Validator facade :

    1. $validator = validator($data, $rules, $messages);

    value() {#collection-method}

    value 函数返回给定的值。但是,如果将一个闭包传递给该函数,则将执行该闭包并返回其结果:

    1. $result = value(true);
    2. // true
    3. $result = value(function () {
    4. return false;
    5. });
    6. // false

    view() {#collection-method}

    view 函数获取一个 视图 实例:

    1. return view('auth.login');

    with() {#collection-method}

    with 函数会返回给定的值。如果传入一个闭包作为该函数的第二个参数,会返回闭包执行的结果:

    1. $callback = function ($value) {
    2. return (is_numeric($value)) ? $value * 2 : 0;
    3. };
    4. $result = with(5, $callback);
    5. // 10
    6. $result = with(null, $callback);
    7. // 0
    8. $result = with(5, null);
    9. // 5

    译者署名

    用户名 头像 职能 签名
    Seven Du Laravel 的辅助函数列表 - 图1 翻译 基于 Laravel 的社交开源系统 ThinkSNS+ 欢迎 Star。
    @JokerLinly Laravel 的辅助函数列表 - 图2 Review Stay Hungry. Stay Foolish.

    {note} 欢迎任何形式的转载,但请务必注明出处,尊重他人劳动共创开源社区。

    转载请注明:本文档由 Laravel China 社区 laravel-china.org 组织翻译,详见 翻译召集帖。

    文档永久地址: https://d.laravel-china.org