coliving/awesome-helpers

我发现这些辅助函数超级实用。

1.0.1 2024-07-12 04:27 UTC

This package is not auto-updated.

Last update: 2024-09-21 03:37:45 UTC


README

Travis CI status image

安装

composer require coliving/awesome-helpers

辅助函数

carbon

快捷方式: new CarbonCarbon::parse()

carbon('One year ago');

chain

使普通对象可链式调用。

chain(new SomeClass)
    ->firstMethod()
    ->secondMethod()
    ->thirdMethod();

// You can use the "carry" constant to pass the result of one method into the other:
chain(new Str)->singular('cars')->ucfirst(carry)();
// Returns "Car"
// Also, you can grab the result of the chain by trailing
// a "()" on the end of it. (Thanks to Taylor Otwell for these two additions)

connection

在不同的数据库连接下运行回调。

$tenantPostIds = connection('tenantdb', function () {
    return Post::pluck('id');
});

dump_sql

返回带有绑定数据的 SQL 查询。

dump_sql(\DB::table('users')->where('email', "blaBla")->where('id', 1));
// returns "select * from `users` where `email` = 'blaBla' and `id` = 1"

faker

快捷方式: $faker = Faker\Factory::create()

faker()->address; // returns random, fake address
faker('address'); // alternate syntax

user

快捷方式: auth()->user()

user()->posts()->create([...]);

money

echo money(12); // echoes "$12.00"
echo money(12.75); // echoes "$12.75"
echo money(12.75, false); // echos "$13"
echo money(12.75, true, 'en_GB'); // echos "£12.75"
// Note: unless specified otherwise, money() will detect the current locale.

ok

快捷方式: response('', 204)。当你从端点没有返回任何内容,但想返回成功。

return ok();

stopwatch

返回提供的回调执行所需的时间(以秒为单位)。对于调试和性能分析很有用。

stopwatch(function () {
    sleep(2);
}); // returns "2.0"

str_between

返回第二个参数之间的字符串。

str_between('--thing--', '--'); // returns "thing"
str_between('[thing]', '[', ']'); // returns "thing"

Str::between('--thing--', '--'); // returns "thing"
Str::between('[thing]', '[', ']'); // returns "thing"

str_extract

返回提供的正则表达式模式中包含的捕获组。

str_extract('Jan-01-2019', '/Jan-(.*)-2019/'); // returns "01"

Str::extract('Jan-01-2019', '/Jan-(.*)-2019/'); // returns "01"

str_match

检查提供的字符串是否与提供的正则表达式模式匹配。

str_match('Jan-01-2019', '/Jan-.*-2019/'); // returns true
str_match('foo bar baz', 'bar'); // returns true

Str::match('Jan-1-2019', '/Jan-(.*)-2019/'); // returns true

str_validate

使用 Laravel 内置验证系统简单验证字符串的一种方式。

str_validate('calebporzio@aol.com', 'regex:/\.net$/|email|max:10');
// returns: ["Format is invalid.", "May not be greater than 10 characters."]

Str::validate('calebporzio@aol.com', 'regex:/\.net$/|email|max:10');
// returns: ["Format is invalid.", "May not be greater than 10 characters."]

str_wrap

str_wrap('thing', '--'); // returns "--thing--"

Str::wrap('thing', '--'); // returns "--thing--"

swap

此函数交换两个变量的值。

$startDate = '2040-01-01';
$endDate = '2020-01-01';

if ($endDate < $startDate) {
    swap($startDate, $endDate);
}

echo $startDate; // prints "2020-01-01"
echo $endDate; // prints "2040-01-01"

tinker

类似于 dd(),但将打开一个带有传递变量的 artisan tinker 终端会话,以便您可以进行操作。

$somethingYouWantToDebug = new StdClass;
tinker($somethingYouWantToDebug);

我遗漏了一个很棒的辅助函数吗?

提交一个包含您使用的辅助函数或对他人有想法的 PR 或问题!

TTFN,Caleb