{"id":36266444,"url":"https://github.com/imliam/laravel-macros","last_synced_at":"2026-01-11T08:04:20.664Z","repository":{"id":56990478,"uuid":"140900904","full_name":"imliam/laravel-macros","owner":"imliam","description":"A collection of miscellaneous methods to extend some of Laravel's core classes through the use of macros and mixins","archived":false,"fork":false,"pushed_at":"2018-07-13T23:27:02.000Z","size":11,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-06-05T09:58:10.777Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/imliam.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-07-13T23:25:13.000Z","updated_at":"2020-07-10T15:08:29.000Z","dependencies_parsed_at":"2022-08-21T13:50:26.964Z","dependency_job_id":null,"html_url":"https://github.com/imliam/laravel-macros","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/imliam/laravel-macros","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/imliam%2Flaravel-macros","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/imliam%2Flaravel-macros/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/imliam%2Flaravel-macros/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/imliam%2Flaravel-macros/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/imliam","download_url":"https://codeload.github.com/imliam/laravel-macros/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/imliam%2Flaravel-macros/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28297988,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-11T04:44:51.577Z","status":"ssl_error","status_checked_at":"2026-01-11T04:44:44.232Z","response_time":60,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2026-01-11T08:04:19.947Z","updated_at":"2026-01-11T08:04:20.653Z","avatar_url":"https://github.com/imliam.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Laravel Macros\n\n[![Latest Version on Packagist](https://img.shields.io/packagist/v/imliam/laravel-macros.svg)](https://packagist.org/packages/imliam/laravel-macros)\n[![Total Downloads](https://img.shields.io/packagist/dt/imliam/laravel-macros.svg)](https://packagist.org/packages/imliam/laravel-macros)\n[![License](https://img.shields.io/github/license/imliam/laravel-macros.svg)](LICENSE.md)\n\nA collection of miscellaneous methods to extend some of Laravel's core classes through the use of [macros and mixins](https://tighten.co/blog/the-magic-of-laravel-macros).\n\n\u003c!-- TOC --\u003e\n\n- [Laravel Macros](#laravel-macros)\n    - [Installation](#installation)\n    - [Usage](#usage)\n        - [Illuminate\\Support\\Collection](#illuminate\\support\\collection)\n            - [`Collection@sortByDate($key = null)`](#collectionsortbydatekey--null)\n            - [`Collection@sortByDateDesc($key = null)`](#collectionsortbydatedesckey--null)\n            - [`Collection@keysToValues()`](#collectionkeystovalues)\n            - [`Collection@valuesToKeys()`](#collectionvaluestokeys)\n        - [Illuminate\\Database\\Query\\Builder](#illuminate\\database\\query\\builder)\n            - [`Builder@if($condition, $column, $operator, $value)`](#builderifcondition-column-operator-value)\n        - [Illuminate\\Http\\Request](#illuminate\\http\\request)\n            - [`Request@replace($key, $value)`](#requestreplacekey-value)\n        - [Illuminate\\Support\\Facades\\Route](#illuminate\\support\\facades\\route)\n            - [`Route@viewDir($path, $viewDirectory = '', $data = [])`](#routeviewdirpath-viewdirectory---data--)\n    - [Testing](#testing)\n    - [Changelog](#changelog)\n    - [Contributing](#contributing)\n        - [Security](#security)\n    - [Credits](#credits)\n    - [License](#license)\n\n\u003c!-- /TOC --\u003e\n\n## Installation\n\nYou can install the package with [Composer](https://getcomposer.org/) using the following command:\n\n```bash\ncomposer require imliam/laravel-macros:^0.1.0\n```\n\n## Usage\n\nOnce installed, all macros will automatically be registered and methods will immediately be available for use.\n\n### Illuminate\\Support\\Collection\n\n#### `Collection@sortByDate($key = null)`\n\nSort the values in a collection by a datetime value.\n\nTo sort a simple list of dates, call the method without passing any arguments to it.\n\n```php\ncollect(['2018-01-04', '1995-07-15', '2000-01-01'])-\u003esortByDate();\n// return collect(['1995-07-15', '2000-01-01', '2018-01-04'])\n```\n\nTo sort a collection where the date is in a specific key, pass the key name when calling the method.\n\n```php\ncollect([\n    ['date' =\u003e '2018-01-04', 'name' =\u003e 'Banana'],\n    ['date' =\u003e '1995-07-15', 'name' =\u003e 'Apple'],\n    ['date' =\u003e '2000-01-01', 'name' =\u003e 'Orange']\n])-\u003esortByDate('date')\n  -\u003eall();\n\n// [\n//    ['date' =\u003e '1995-07-15', 'name' =\u003e 'Apple'],\n//    ['date' =\u003e '2000-01-01', 'name' =\u003e 'Orange'],\n//    ['date' =\u003e '2018-01-04', 'name' =\u003e 'Banana']\n// ]\n```\n\nAdditionally, you can pass a callback to the method to choose more precisely what is sorted.\n\n```php\n$users = User::all();\n\n$users-\u003esortByDate(function(User $user) {\n    return $user-\u003ecreated_at;\n})-\u003etoArray();\n\n// [\n//    ['id' =\u003e 12, 'username' =\u003e 'spatie', 'created_at' =\u003e '1995-07-15'],\n//    ['id' =\u003e 15, 'username' =\u003e 'taylor', 'created_at' =\u003e '2000-01-01'],\n//    ['id' =\u003e 2, 'username' =\u003e 'jeffrey', 'created_at' =\u003e '2018-01-04']\n// ]\n```\n\n#### `Collection@sortByDateDesc($key = null)`\n\nThis method has the same signature as the `sortByDate` method, but will sort the collection in the opposite order.\n\n#### `Collection@keysToValues()`\n\nChange the collection so that all values are equal to the corresponding key.\n\n```php\ncollect(['a' =\u003e 'b', 'c' =\u003e 'd'])-\u003ekeysToValues();\n// ['a' =\u003e 'a', 'c' =\u003e 'c']\n```\n\n#### `Collection@valuesToKeys()`\n\nChange the collection so that all keys are equal to their corresponding value.\n\n```php\ncollect(['a' =\u003e 'b', 'c' =\u003e 'd'])-\u003evaluesToKeys();\n// ['b' =\u003e 'b', 'd' =\u003e 'd']\n```\n\n### Illuminate\\Database\\Query\\Builder\n\n#### `Builder@if($condition, $column, $operator, $value)`\n\nConditionally add where clause to the query builder. [See Mohamed Said's blog post for more information.](https://themsaid.com/laravel-query-conditions-20160425)\n\nKeep chaining methods onto a query being built without having to break it up. Take code like this:\n\n```php\n$results = DB::table('orders')\n    -\u003ewhere('branch_id', Auth::user()-\u003ebranch_id);\n\nif($request-\u003ecustomer_id){\n    $results-\u003ewhere('customer_id', $request-\u003ecustomer_id);\n}\n\n$results = $results-\u003eget();\n```\n\nAnd clean it up into this:\n\n```php\n$results = DB::table('orders')\n    -\u003ewhere('branch_id', Auth::user()-\u003ebranch_id)\n    -\u003eif($request-\u003ecustomer_id, 'customer_id', '=', $request-\u003ecustomer_id)\n    -\u003eget();\n```\n\n### Illuminate\\Http\\Request\n\n#### `Request@replace($key, $value)`\n\nManipulate the request object by replacing a value, or even adding a new one.\n\n```php\nclass Middleware\n{\n    public function handle($request, \\Closure $next)\n    {\n        $request-\u003ereplace('key', 'value');\n\n        return $next($request);\n    }\n}\n```\n\n### Illuminate\\Support\\Facades\\Route\n\n#### `Route@viewDir($path, $viewDirectory = '', $data = [])`\n\nMimics 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.\n\nThis 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.\n\nFor an example, to see how it works, imagine the following route definition:\n\n```php\nRoute::viewDir('/pages', 'pages');\n```\n\nAnd the following directory structure for the views:\n\n```\nviews/\n├── auth/\n├── errors/\n├── layouts/\n├── pages/\n│   ├── about-us.blade.php\n│   ├── faq.blade.php\n│   ├── privacy-policy.blade.php\n│   ├── team/\n│   │   ├── developers.blade.php\n│   │   ├── index.blade.php\n│   │   ├── management.blade.php\n│   │   └── marketing.blade.php\n│   └── terms-of-service.blade.php\n└── partials/\n```\n\nThe following routes will be generated to match each of the views in the given directory:\n\n```\n/pages/about-us\n/pages/faq\n/pages/privacy-policy\n/pages/team\n/pages/team/developers\n/pages/team/management\n/pages/team/marketing\n/pages/terms-of-service\n```\n\n## Testing\n\n``` bash\ncomposer test\n```\n\n## Changelog\n\nPlease see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.\n\n## Contributing\n\nPlease see [CONTRIBUTING](CONTRIBUTING.md) for details.\n\n### Security\n\nIf you discover any security related issues, please email liam@liamhammett.com instead of using the issue tracker.\n\n## Credits\n\n- [Liam Hammett](https://github.com/imliam)\n- [All Contributors](../../contributors)\n\n## License\n\nThe MIT License (MIT). Please see [License File](LICENSE.md) for more information.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fimliam%2Flaravel-macros","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fimliam%2Flaravel-macros","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fimliam%2Flaravel-macros/lists"}