{"id":13593150,"url":"https://github.com/coderflexx/laravel-presenter","last_synced_at":"2025-04-06T03:07:40.347Z","repository":{"id":38356138,"uuid":"458325792","full_name":"coderflexx/laravel-presenter","owner":"coderflexx","description":"Decorate Your Models and Write Clean/Reusable Code with Presenters.","archived":false,"fork":false,"pushed_at":"2025-01-27T19:04:55.000Z","size":128,"stargazers_count":29,"open_issues_count":1,"forks_count":7,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-03-30T02:05:41.820Z","etag":null,"topics":["eloquent","laravel","php","presenters"],"latest_commit_sha":null,"homepage":"https://coderflex.com/open-source/docs/laravel-presenter/introduction","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/coderflexx.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":".github/CONTRIBUTING.md","funding":".github/FUNDING.yml","license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":".github/SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":"coderflex"}},"created_at":"2022-02-11T20:07:09.000Z","updated_at":"2025-01-29T16:31:09.000Z","dependencies_parsed_at":"2023-09-26T00:57:48.460Z","dependency_job_id":"f00c1fa4-7fec-40a0-9d54-8bf2f3024f58","html_url":"https://github.com/coderflexx/laravel-presenter","commit_stats":{"total_commits":72,"total_committers":5,"mean_commits":14.4,"dds":0.4444444444444444,"last_synced_commit":"ce5152c14b468b8d7c4d1afb6e418fff34aa2786"},"previous_names":[],"tags_count":15,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/coderflexx%2Flaravel-presenter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/coderflexx%2Flaravel-presenter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/coderflexx%2Flaravel-presenter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/coderflexx%2Flaravel-presenter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/coderflexx","download_url":"https://codeload.github.com/coderflexx/laravel-presenter/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247427006,"owners_count":20937201,"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":["eloquent","laravel","php","presenters"],"created_at":"2024-08-01T16:01:17.061Z","updated_at":"2025-04-06T03:07:40.325Z","avatar_url":"https://github.com/coderflexx.png","language":"PHP","funding_links":["https://github.com/sponsors/coderflex"],"categories":["PHP"],"sub_categories":[],"readme":"# Laravel Presenter\n\n[![The Latest Version on Packagist](https://img.shields.io/packagist/v/coderflexx/laravel-presenter.svg?style=flat-square)](https://packagist.org/packages/coderflexx/laravel-presenter)\n[![GitHub Tests Action Status](https://img.shields.io/github/actions/workflow/status/coderflexx/laravel-presenter/run-tests.yml?branch=main\u0026label=tests)](https://github.com/coderflexx/laravel-presenter/actions?query=workflow%3Arun-tests+branch%3Amain)\n[![GitHub Code Style Action Status](https://img.shields.io/github/actions/workflow/status/coderflexx/laravel-presenter/phpstan.yml?branch=main\u0026label=code%20style)](https://github.com/coderflexx/laravel-presenter/actions?query=workflow%3A\"Check+%26+fix+styling\"+branch%3Amain)\n[![Total Downloads](https://img.shields.io/packagist/dt/coderflexx/laravel-presenter.svg?style=flat-square)](https://packagist.org/packages/coderflexx/laravel-presenter)\n\nA clean way to present your model attributes without putting them in the wrong file.\n\n\n- [Laravel Presenter](#laravel-presenter)\n  - [Installation](#installation)\n  - [Usage](#usage)\n  - [Model Implementation](#model-implementation)\n  - [Create New Model Presenter class](#create-new-model-presenter-class)\n  - [Using the `Presenter` Generated Class](#using-the-presenter-generated-class)\n  - [Example](#example)\n  - [Adding Another Presenter Type](#adding-another-presenter-type)\n  - [Testing](#testing)\n  - [Changelog](#changelog)\n  - [Contributing](#contributing)\n  - [Security Vulnerabilities](#security-vulnerabilities)\n  - [Credits](#credits)\n  - [License](#license)\n\n## Installation\n\nYou can install the package via composer:\n\n```bash\ncomposer require coderflexx/laravel-presenter\n```\n\nYou can publish the config file with:\n\n```bash\nphp artisan vendor:publish --provider=\"Coderflex\\LaravelPresenter\\LaravelPresenterServiceProvider\"\n```\n\nThis is the contents of the published config file:\n\n```php\nreturn [\n\n    /*\n    |--------------------------------------------------------------------------\n    | Presenter Namespace\n    |--------------------------------------------------------------------------\n    |\n    | This value informs LaravelPresenter which namespace you will be \n    | selecting to store your presenters by default.\n    | If this value equals to null, \"App\\Presenter\" will be used \n    | by default.\n    |\n    */\n\n    'presenter_namespace'   =\u003e 'App\\\\Presenters',\n];\n```\n\n## Usage\nThe implementation of this package is so simple, all what you need to do is the following:\n\n## Model Implementation\n\n-  Implement `CanPresent` Interface \n-  Use `UsesPresenters` Trait\n\n```php\nuse Coderflex\\LaravelPresenter\\Concerns\\CanPresent;\nuse Coderflex\\LaravelPresenter\\Concerns\\UsesPresenters;\n// ...\n\nclass User extends Authenticatable implements CanPresent\n{\n    use UsesPresenters;\n\n    // ...\n}\n```\n\n## Create New Model Presenter class\n\nThis package gives you an easy way to generate new `Presenter` class, all you need to do is to use `presenter:make` command.\n\n```bash\nphp artisan presenter:make UserPresenter\n```\n\n`UserPresenter` in our case, leaves by default in `App\\Presenters`.\n\nThis is the contents of the `UserPresenter` file:\n\n```php\n\u003c?php\n\nnamespace App\\Presenters;\n\nuse Coderflex\\LaravelPresenter\\Presenter;\n\nclass UserPresenter extends Presenter\n{\n    // \n}\n\n```\n\n\nIf you want to change the directory, you have two options.\n\nFirst options is to set the full namespace while you're creating the presenter class\n\n```bash\nphp artisna presenter:make App\\Models\\Presenter\\UserPresenter\n```\n\nOr change `presenter_namespace` from `config/laravel-presenter` file.\n\n```php\nreturn [\n    ...\n\n    'presenter_namespace'   =\u003e 'App\\\\Presenters',\n\n    ...\n];\n```\n\n## Using the `Presenter` Generated Class\nAfter you create the presenter class, you need to register it on the `Model` by adding the `$presenters` protected property:\n\n```php\nuse App\\Presenters\\UserPresenter;\nuse Coderflex\\LaravelPresenter\\Concerns\\CanPresent;\nuse Coderflex\\LaravelPresenter\\Concerns\\UsesPresenters;\n// ...\n\nclass User extends Authenticatable implements CanPresent\n{\n    use UsesPresenters;\n\n    protected $presenters = [\n        'default' =\u003e UserPresenter,\n    ];\n}\n```\nBy default, the type of your presenter class is `default`, but you can use as many of presenters you want, just by identifying the type in `$presenters` property.\n\n## Example\nNow, after we generated the `presenter` class, and we implemented it successfully in our model, we can use it like so:\n\nIn your `UserPresenter` class or any presenter class you generated.\n\n```php\n...\nclass UserPresenter extends Presenter\n{\n    public function fullName()\n    {\n        return \"{$this-\u003emodel-\u003efirst_name} {$this-\u003emodel-\u003elast_name}\";\n    }\n}\n...\n```\n\nWe add a new method to present the `fullName`.\n\nIn your blade or any place you want, you can do:\n\n```php\n$user-\u003epresent()-\u003efullName\n```\nYour application will show the full name from the method you added.\n\n## Adding Another Presenter Type\nLike I said above, by default the type will be `default` but, you can add more types as you need.\n\nHere is an example:\n\n```php\nuse App\\Presenters\\UserPresenter;\nuse Coderflex\\LaravelPresenter\\Concerns\\CanPresent;\nuse Coderflex\\LaravelPresenter\\Concerns\\UsesPresenters;\n// ...\n\nclass User extends Authenticatable implements CanPresent\n{\n    use UsesPresenters;\n\n    protected $presenters = [\n        'default' =\u003e UserPresenter,\n        'setting' =\u003e UserSettingPresenter,\n    ];\n}\n```\n\nGenerate new `UserSettingPresenter`\n\n```bash\nphp artisan presenter:make UserSettingPresenter\n```\n\nAdd anything to `UserSettingPresenter` method\n\n```php\n...\nclass UserSettingPresenter extends Presenter\n{\n    public function lang()\n    {\n        return $this-\u003emodel-\u003esettings-\u003edefaultLang;\n    }\n}\n...\n```\n\nFinally, set `setting` as a type:\n\n```php\n$user-\u003epresent('setting')-\u003elang;\n```\n\nBy that, you can split your logic and make your code base even cleaner.\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](.github/CONTRIBUTING.md) for details.\n\n## Security Vulnerabilities\n\nPlease review [our security policy](../../security/policy) on how to report security vulnerabilities.\n\n## Credits\n\n- [Oussama Sid](https://github.com/ousid)\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%2Fcoderflexx%2Flaravel-presenter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcoderflexx%2Flaravel-presenter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcoderflexx%2Flaravel-presenter/lists"}