{"id":20062736,"url":"https://github.com/tooleks/laravel-presenter","last_synced_at":"2026-04-28T16:37:34.875Z","repository":{"id":57071705,"uuid":"75742839","full_name":"tooleks/laravel-presenter","owner":"tooleks","description":"The Laravel Presenter Composer Package","archived":false,"fork":false,"pushed_at":"2017-06-02T15:26:47.000Z","size":83,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-08-17T03:52:23.898Z","etag":null,"topics":["collection","composer","data","entity","laravel","mapper","mapping","php","presenter","representation","view"],"latest_commit_sha":null,"homepage":"https://packagist.org/packages/tooleks/laravel-presenter","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/tooleks.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-12-06T15:10:50.000Z","updated_at":"2017-06-07T12:24:56.000Z","dependencies_parsed_at":"2022-08-24T14:54:32.451Z","dependency_job_id":null,"html_url":"https://github.com/tooleks/laravel-presenter","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"purl":"pkg:github/tooleks/laravel-presenter","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tooleks%2Flaravel-presenter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tooleks%2Flaravel-presenter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tooleks%2Flaravel-presenter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tooleks%2Flaravel-presenter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tooleks","download_url":"https://codeload.github.com/tooleks/laravel-presenter/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tooleks%2Flaravel-presenter/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32390053,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-28T14:34:11.604Z","status":"ssl_error","status_checked_at":"2026-04-28T14:32:37.009Z","response_time":56,"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":["collection","composer","data","entity","laravel","mapper","mapping","php","presenter","representation","view"],"created_at":"2024-11-13T13:38:39.737Z","updated_at":"2026-04-28T16:37:34.858Z","avatar_url":"https://github.com/tooleks.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# The Laravel Presenter Package\n\nThe package provides the `Presenter` layer for wrapping model objects into new presentations.\n\n## Features\n\nThe package supports:\n\n* Objects and arrays presentation\n* Data mapping\n* Nested attributes\n* Attributes overriding\n* The Laravel 5.2 collections\n* JSON serialization\n* Casting to array/JSON\n* Injections into the constructor\n\n## Requirements\n\n\"php\": \"^7.0\",\n\"illuminate/support\": \"^5.2\",\n\"illuminate/contracts\": \"^5.2\"\n\n## Installation\n\n### Package Installation\n\nExecute the following command to get the latest version of the package:\n\n```shell\ncomposer require tooleks/laravel-presenter\n```\n\n### App Configuration\n\nTo register the service provider simply add the `Tooleks\\Laravel\\Presenter\\Providers\\PresenterProvider::class` into your `config/app.php` to the end of the `providers` array:\n\n```php\n'providers' =\u003e [\n    ...\n    Tooleks\\Laravel\\Presenter\\Providers\\PresenterProvider::class,\n],\n```\n\n\n## Usage Examples\n\n### Model Presentation\n\nTo define your presenter class, you need to extend base `Tooleks\\Laravel\\Presenter\\Presenter` class, as shown in the example below.\n\nOverride the `getAttributesMap()` method to build attributes map definition.\n\n```php\n\u003c?php\n\nnamespace App\\Presenters;\n\nuse Tooleks\\Laravel\\Presenter\\Presenter;\n\n/**\n * Class UserPresenter.\n *\n * @property string nickname\n * @property string short_name\n * @property string full_name\n * @property string role\n */\nclass UserPresenter extends Presenter\n{\n    /**\n     * @inheritdoc\n     */\n    protected function getAttributesMap(): array\n    {\n        return [\n            // 'presenter_attribute_name' =\u003e 'wrapped_model_attribute_name'\n            'nickname' =\u003e 'username',\n            'short_name' =\u003e 'first_name',\n            'full_name' =\u003e function () {\n                return $this-\u003egetWrappedModelAttribute('first_name') . ' ' . $this-\u003egetWrappedModelAttribute('last_name');\n            },\n            'role' =\u003e 'role.name',\n        ];\n    }\n}\n```\n\nCreate a presenter object instance by passing a wrapped model into a `setWrappedModel` method and use it like an object with `nickname`, `short_name`, `full_name`, `role` attributes. The wrapped model may be an `array` or an `object`.\n\n```php\n\u003c?php\n\n$user = [ \n    'username' =\u003e 'anna',\n    'first_name' =\u003e 'Anna',\n    'last_name' =\u003e 'P.',\n    'role' =\u003e [\n        'name' =\u003e 'User',\n    ],\n];\n\n$userPresenter = app()-\u003emake(\\App\\Presenters\\UserPresenter::class)-\u003esetWrappedModel($user);\n// Create the presenter from the wrapped model array.\n\n$userPresenter = app()-\u003emake(\\App\\Presenters\\UserPresenter::class)-\u003esetWrappedModel((object) $user);\n// Create the presenter from the wrapped model object.\n\necho $userPresenter-\u003enickname;\n// Prints 'anna' string, as we mapped the wrapped model 'username' attribute to the presenter 'nickname' attribute.\necho $userPresenter-\u003eshort_name;\n// Prints 'Anna' string, as we mapped the wrapped model 'first_name' attribute to the presenter 'short_name' attribute.\necho $userPresenter-\u003efull_name;\n// Prints 'Anna P.' string, as we override the presenter 'full_name' attribute by the anonymous function.\necho $userPresenter-\u003erole;\n// Prints 'User' string, as we mapped the wrapped model 'role.name' nested attribute to the presenter 'role' attribute.\n```\n\n### Collection Presentation\n\nThe package also provides the collection macros method `present()` for wrapping each item in the collection into a presenter class.\n\n```php\n\u003c?php\n\ncollect([$user])-\u003epresent(\\App\\Presenters\\UserPresenter::class);\n// Create the collection of the 'UserPresenter' items.\n```\n\n### Data Mapping\n\nThe package provides a simple mechanism for data mapping. All you need is to call `toArray()` method on the presenter object instance.\n\n```php\n\u003c?php\n\n$mappedDataArray = app()-\u003emake(\\App\\Presenters\\UserPresenter::class)-\u003esetWrappedModel($user)-\u003etoArray();\n\n$mappedDataObject = (object) app()-\u003emake(\\App\\Presenters\\UserPresenter::class)-\u003esetWrappedModel($user)-\u003etoArray();\n```\n\n### Using as a Response Data\n\nThe `Tooleks\\Laravel\\Presenter\\Presenter` class implements `Illuminate\\Contracts\\Support\\Arrayable`, `Illuminate\\Contracts\\Support\\Jsonable`, `JsonSerializable` interfaces, so you may pass objects of this class directly into the response.\n\n```php\n\u003c?php\n\n$content = app()-\u003emake(\\App\\Presenters\\UserPresenter::class)-\u003esetWrappedModel($user);\n\nresponse($content);\n```\n\n## Tests\n\nExecute the following command to run tests:\n\n```shell\n./vendor/bin/phpunit\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftooleks%2Flaravel-presenter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftooleks%2Flaravel-presenter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftooleks%2Flaravel-presenter/lists"}