imliam/laravel-macros
Composer 安装命令:
composer require imliam/laravel-macros
包简介
A collection of miscellaneous methods to extend some of Laravel's core classes through the use of macros and mixins
README 文档
README
A collection of miscellaneous methods to extend some of Laravel's core classes through the use of macros and mixins.
- Laravel Macros
Installation
You can install the package with Composer using the following command:
composer require imliam/laravel-macros:^0.1.0
Usage
Once installed, all macros will automatically be registered and methods will immediately be available for use.
Illuminate\Support\Collection
Collection@sortByDate($key = null)
Sort the values in a collection by a datetime value.
To sort a simple list of dates, call the method without passing any arguments to it.
collect(['2018-01-04', '1995-07-15', '2000-01-01'])->sortByDate(); // return collect(['1995-07-15', '2000-01-01', '2018-01-04'])
To sort a collection where the date is in a specific key, pass the key name when calling the method.
collect([ ['date' => '2018-01-04', 'name' => 'Banana'], ['date' => '1995-07-15', 'name' => 'Apple'], ['date' => '2000-01-01', 'name' => 'Orange'] ])->sortByDate('date') ->all(); // [ // ['date' => '1995-07-15', 'name' => 'Apple'], // ['date' => '2000-01-01', 'name' => 'Orange'], // ['date' => '2018-01-04', 'name' => 'Banana'] // ]
Additionally, you can pass a callback to the method to choose more precisely what is sorted.
$users = User::all(); $users->sortByDate(function(User $user) { return $user->created_at; })->toArray(); // [ // ['id' => 12, 'username' => 'spatie', 'created_at' => '1995-07-15'], // ['id' => 15, 'username' => 'taylor', 'created_at' => '2000-01-01'], // ['id' => 2, 'username' => 'jeffrey', 'created_at' => '2018-01-04'] // ]
Collection@sortByDateDesc($key = null)
This method has the same signature as the sortByDate method, but will sort the collection in the opposite order.
Collection@keysToValues()
Change the collection so that all values are equal to the corresponding key.
collect(['a' => 'b', 'c' => 'd'])->keysToValues(); // ['a' => 'a', 'c' => 'c']
Collection@valuesToKeys()
Change the collection so that all keys are equal to their corresponding value.
collect(['a' => 'b', 'c' => 'd'])->valuesToKeys(); // ['b' => 'b', 'd' => 'd']
Illuminate\Database\Query\Builder
Builder@if($condition, $column, $operator, $value)
Conditionally add where clause to the query builder. See Mohamed Said's blog post for more information.
Keep chaining methods onto a query being built without having to break it up. Take code like this:
$results = DB::table('orders') ->where('branch_id', Auth::user()->branch_id); if($request->customer_id){ $results->where('customer_id', $request->customer_id); } $results = $results->get();
And clean it up into this:
$results = DB::table('orders') ->where('branch_id', Auth::user()->branch_id) ->if($request->customer_id, 'customer_id', '=', $request->customer_id) ->get();
Illuminate\Http\Request
Request@replace($key, $value)
Manipulate the request object by replacing a value, or even adding a new one.
class Middleware { public function handle($request, \Closure $next) { $request->replace('key', 'value'); return $next($request); } }
Illuminate\Support\Facades\Route
Route@viewDir($path, $viewDirectory = '', $data = [])
Mimics the functionality offered by Route::view() method but extends it by rerouting requested the URI at any number of sub-levels to match a view directory in the code base.
This makes it possible to create views with static content and not need to worry about updating routes to match them or using a CMS-style solution to manage them.
For an example, to see how it works, imagine the following route definition:
Route::viewDir('/pages', 'pages');
And the following directory structure for the views:
views/
├── auth/
├── errors/
├── layouts/
├── pages/
│ ├── about-us.blade.php
│ ├── faq.blade.php
│ ├── privacy-policy.blade.php
│ ├── team/
│ │ ├── developers.blade.php
│ │ ├── index.blade.php
│ │ ├── management.blade.php
│ │ └── marketing.blade.php
│ └── terms-of-service.blade.php
└── partials/
The following routes will be generated to match each of the views in the given directory:
/pages/about-us
/pages/faq
/pages/privacy-policy
/pages/team
/pages/team/developers
/pages/team/management
/pages/team/marketing
/pages/terms-of-service
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security
If you discover any security related issues, please email liam@liamhammett.com instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.
imliam/laravel-macros 适用场景与选型建议
imliam/laravel-macros 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 6 次下载、GitHub Stars 达 3, 最近一次更新时间为 2018 年 07 月 13 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「mixins」 「macros」 「laravel-macros」 「imliam」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 imliam/laravel-macros 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 imliam/laravel-macros 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 imliam/laravel-macros 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Custom Laravel Collection macros.
Laravel Str mixins
Laravel builder match against eloquent syntax and related.
A useful set of HTML and Form macros with css and js made for bootstrap
Generate phpDoc for laravel macroable class.
Various helpers and tools for Laravel
统计信息
- 总下载量: 6
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 6
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-07-13