在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):sebastiaanluca/laravel-helpers开源软件地址(OpenSource Url):https://github.com/sebastiaanluca/laravel-helpers开源编程语言(OpenSource Language):PHP 100.0%开源软件介绍(OpenSource Introduction):An extensive set of Laravel framework helper functions and collection macros. Table of contents
Requirements
How to installJust add the package to your project using Composer and Laravel will auto-discover it: composer require sebastiaanluca/laravel-helpers If you want to use the collection debug macros, install the kint-php/kint package as a dev dependency: composer require kint-php/kint --dev Upgrading from 1.xAll essential generic PHP helpers have been extracted to their own sebastiaanluca/php-helpers package and some other helpers have been removed in anticipation of their own package. In effect and from now on, Laravel Helpers will only contain helpers for the Laravel framework. See the changelog for more information. Framework helper functionslocaleGet the active app locale or the fallback locale if it's missing or not set. locale();
// "en" is_guestDetermine if the current user is a guest. The opposite of is_logged_in. // When not authenticated
is_guest();
// true
// When authenticated as a user
is_guest();
// false is_logged_inDetermine if the current user is authenticated. The opposite of is_guest. // When not authenticated
is_logged_in();
// false
// When authenticated as a user
is_logged_in();
// true userGet the currently authenticated user (if there is one). When logged in, returns your user model or object that implements // When not authenticated
user();
// null
// When authenticated as a user
user();
// Illuminate\Foundation\Auth\User {} meGet the currently authenticated user (if there is one). When logged in, returns your user model or object that implements An alternative for user. // When not authenticated
me();
// null
// When authenticated as a user
me();
// Illuminate\Foundation\Auth\User {} Collection macroscarbonizeCreate Carbon instances from items in a collection. collect([
'yesterday',
'tomorrow',
'2017-07-01',
])->carbonize();
/*
Illuminate\Support\Collection {
all: [
Carbon\Carbon {
"date": "2017-07-09 00:00:00.000000",
"timezone_type": 3,
"timezone": "UTC",
},
Carbon\Carbon {
"date": "2017-07-11 00:00:00.000000",
"timezone_type": 3,
"timezone": "UTC",
},
Carbon\Carbon {
"date": "2017-07-01 00:00:00.000000",
"timezone_type": 3,
"timezone": "UTC",
},
],
}
*/ betweenReduce each collection item to the value found between a given start and end string. The second parameter is optional and falls back to the start string if collect([
'"value1"',
'"value2"',
'"value3"',
])->between('"', '"');
/*
Illuminate\Support\Collection {
all: [
"value1",
"value2",
"value3",
],
}
*/ transformKeysPerform an operation on the collection's keys. The callable operation can either be a globally available method or a closure. collect([
'a' => 'value',
'b' => 'value',
'c' => 'value',
])->transformKeys('strtoupper');
/*
Illuminate\Support\Collection {
all: [
"A" => "value",
"B" => "value",
"C" => "value",
],
}
*/ collect([
'a' => 'value',
'b' => 'value',
'c' => 'value',
])->transformKeys(function (string $key) {
return 'prefix-' . $key;
});
/*
Illuminate\Support\Collection {
all: [
"prefix-a" => "value",
"prefix-b" => "value",
"prefix-c" => "value",
],
}
*/ transposeTranspose (flip) a collection matrix (array of arrays) so its columns become rows and its rows become columns. collect([
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
])->transpose();
/*
Illuminate\Support\Collection {
all: [
[1, 4, 7],
[2, 5, 8],
[3, 6, 9],
],
}
*/ transposeWithKeysFlip a collection of rows and values per column so its columns become rows and its rows become columns. Before:
After:
How to use: collect([
'A' => [
'id' => 1,
'name' => 'James',
],
'B' => [
'id' => 2,
'name' => 'Joe',
],
'C' => [
'id' => 3,
'name' => 'Jonas',
],
])->transposeWithKeys();
/*
Illuminate\Support\Collection {
all: [
"id" => [
"A" => 1,
"B" => 2,
"C" => 3,
],
"name" => [
"A" => "James",
"B" => "Joe",
"C" => "Jonas",
],
],
}
*/ You can also pass some row header names if you don't want them to be automatically guessed. You'd then call the macro with dDisplay structured debug information on the collection using Kint. Can be called multiple times during a collection's method chain and outputs debug information at each point of use. Continues script execution afterwards. Explicitly requires the kint-php/kint package. collect([
'id' => 6,
'name' => 'Sebastiaan',
])
->d()
->put('role', 'author')
->d(); dddDisplay structured debug information on the collection using Kint. Halts script execution afterwards, so it can only be called once during a collection's method chain. Explicitly requires the kint-php/kint package. collect([
'id' => 6,
'name' => 'Sebastiaan',
])
->d()
->put('role', 'author')
->ddd(); LicenseThis package operates under the MIT License (MIT). Please see LICENSE for more information. Change logPlease see CHANGELOG for more information what has changed recently. Testingcomposer install
composer test ContributingPlease see CONTRIBUTING and CODE OF CONDUCT for details. SecurityIf you discover any security related issues, please email [email protected] instead of using the issue tracker. CreditsAboutMy name is Sebastiaan and I'm a freelance Laravel developer specializing in building custom Laravel applications. Check out my portfolio for more information, my blog for the latest tips and tricks, and my other packages to kick-start your next project. Have a project that could use some guidance? Send me an e-mail at [email protected]! |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论