{"id":23512765,"url":"https://github.com/baethon/laravel-resource","last_synced_at":"2025-04-19T12:18:00.443Z","repository":{"id":52244055,"uuid":"352054952","full_name":"baethon/laravel-resource","owner":"baethon","description":"Conveniant factory for Laravel API resources","archived":false,"fork":false,"pushed_at":"2023-01-05T14:37:46.000Z","size":174,"stargazers_count":4,"open_issues_count":4,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-09T21:09:00.582Z","etag":null,"topics":["api","http","laravel","resource","resources"],"latest_commit_sha":null,"homepage":"","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/baethon.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-03-27T11:25:21.000Z","updated_at":"2023-01-05T14:36:38.000Z","dependencies_parsed_at":"2023-02-04T06:31:34.984Z","dependency_job_id":null,"html_url":"https://github.com/baethon/laravel-resource","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/baethon%2Flaravel-resource","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/baethon%2Flaravel-resource/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/baethon%2Flaravel-resource/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/baethon%2Flaravel-resource/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/baethon","download_url":"https://codeload.github.com/baethon/laravel-resource/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248952351,"owners_count":21188426,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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":["api","http","laravel","resource","resources"],"created_at":"2024-12-25T13:19:23.824Z","updated_at":"2025-04-19T12:18:00.423Z","avatar_url":"https://github.com/baethon.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# baethon/laravel-resource\n\n![Example](https://raw.githubusercontent.com/baethon/laravel-resource/master/example.png)\n\nThe package provides a convenient factory function for [Laravel API Resources](https://laravel.com/docs/8.x/eloquent-resources). Based on the given model, it will try to find the corresponding API resource and return it. If the resource doesn't exist, it will use the base `JsonResource` class. It works with collections.\n\n## Installation\n\n```\ncomposer require baethon/laravel-resource\n```\n\n## Example usage\n\n```php\n\u003c?php\n\nnamespace App\\Http\\Controllers;\n\nuse function Baethon\\Laravel\\Resource\\resource;\n\nclass UserController\n{\n    public function show(\\App\\Models\\User $user)\n    {\n        return resource($user);\n    }\n}\n```\n\nFactory will work also with conditional loads:\n\n```php\n\u003c?php\n\nnamespace App\\Http\\Resources;\n\nuse function Baethon\\Laravel\\Resource\\resource;\n\nclass UserResource extends \\Illuminate\\Http\\Resources\\Json\\JsonResource\n{\n    public function toArray($request)\n    {\n        return [\n            'tags' =\u003e resource($this-\u003ewhenLoaded('tags')),\n        ];\n    }\n}\n```\n\n## How it works?\n\nThe package tries to follow the Laravels naming conventions. When creating a resource for `User` model, it will look for `UserResource`. When passing a collection of `User` instances, it will look for `UserResourceCollection` or `UserCollection`.\n\nIn the case of collections, the package won't wrap individual models in their respective resources. It will pass that responsibility to the collection resource instead.\n\n## Rationale\n\nWhen you decide to use API resources, you should create an individual resource for each model returned by the API. In many cases, they're not extended in any way, and it seems pointless to create a bunch of empty classes.\n\nTo avoid this, you might use the `JsonResource` for the \"generic\" models and create resources only for those models that include any logic. However, when the time comes to make a customized resource for one of the models, you'll have to update all the places where you previously used the `JsonResource`.\n\nThis is the moment when `resource()` shines. You simply need to create the customized resource, and the factory will automatically start using it instead of the base resource.\n\n## Customization\n\nThere are two ways to customize the factory:\n1. custom map\n1. custom naming strategy\n\nTo change any of them, you'll have to publish the config files:\n\n```\nphp artisan vendor:publish --provider=\"Baethon\\Laravel\\Resource\\ServiceProvider\"\n```\n## License \n\nThe package is open-sourced software licensed under the [MIT license](http://opensource.org/licenses/MIT).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbaethon%2Flaravel-resource","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbaethon%2Flaravel-resource","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbaethon%2Flaravel-resource/lists"}