calebporzio / awesome-helpers
此包已被废弃,不再维护。未建议替代包。
我发现这些助手函数非常实用
v2.5.0
2021-01-20 17:40 UTC
Requires
- php: >=7.3
Requires (Dev)
- fakerphp/faker: ^1.12
- illuminate/support: 5.7.*|5.8.*|^6.0|^7.0|^8.0
- mockery/mockery: dev-master
- phpunit/phpunit: ^8.0|^9.0
README
安装
composer require calebporzio/awesome-helpers
助手函数
carbon
快捷方式:new Carbon
或 Carbon::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或问题!
再见,Caleb