{"id":20504443,"url":"https://github.com/topclaudy/eloquent-mutators","last_synced_at":"2025-04-10T00:29:05.622Z","repository":{"id":38094925,"uuid":"161528822","full_name":"topclaudy/eloquent-mutators","owner":"topclaudy","description":"Reusable accessors/mutators (getters/setters) for Laravel's Eloquent","archived":false,"fork":false,"pushed_at":"2024-08-03T17:22:50.000Z","size":239,"stargazers_count":46,"open_issues_count":6,"forks_count":6,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-01-26T15:08:44.530Z","etag":null,"topics":[],"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/topclaudy.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-12-12T18:29:08.000Z","updated_at":"2024-11-30T01:09:24.000Z","dependencies_parsed_at":"2024-08-03T18:51:13.090Z","dependency_job_id":null,"html_url":"https://github.com/topclaudy/eloquent-mutators","commit_stats":{"total_commits":59,"total_committers":5,"mean_commits":11.8,"dds":0.3389830508474576,"last_synced_commit":"b0db61b75d876160cc3a31ab0c3083da15c181f7"},"previous_names":[],"tags_count":23,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/topclaudy%2Feloquent-mutators","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/topclaudy%2Feloquent-mutators/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/topclaudy%2Feloquent-mutators/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/topclaudy%2Feloquent-mutators/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/topclaudy","download_url":"https://codeload.github.com/topclaudy/eloquent-mutators/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":236815282,"owners_count":19209198,"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":[],"created_at":"2024-11-15T19:37:59.307Z","updated_at":"2025-02-02T16:14:01.979Z","avatar_url":"https://github.com/topclaudy.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"Eloquent Mutators\n==================\n\n**Eloquent Mutators** allows us to define accessors and mutators outside of an Eloquent model. This gives us the ability to organize and reuse them on any model or any attribute of the same model.\n\n## The problem\n\nEloquent has support for [accessors and mutators](https://laravel.com/docs/5.7/eloquent-mutators). However, it requires us to define them directly in the model. What if we want to reuse an accessor/mutator logic in another model? Or, what if we want to reuse an accessor/mutator logic for another attribute of the same model? We can't! **Eloquent Mutators** aims at solving this limitation.\n\n#### Related discussions:\n\n* [Reusing accessors \u0026 mutators](https://stackoverflow.com/questions/37725691/reusing-an-accessors-mutators/37727418#37727418)\n* [Simple and organized accessors \u0026 mutators](https://github.com/laravel/ideas/issues/1270)\n\n## Installation\n\nThe recommended way to install **Eloquent Mutators** is through [Composer](http://getcomposer.org/)\n\n```bash\n$ composer require awobaz/eloquent-mutators\n```\n\nThe package will automatically register itself if you're using Laravel 5.5+. For Laravel 5.4, you'll have to register the package manually:\n\n1) Open your `config/app.php` and add the following to the `providers` array:\n\n```php\nAwobaz\\Mutator\\MutatorServiceProvider::class,\n```\n\n2) In the same `config/app.php` add the following to the `aliases ` array: \n\n```php\n'Mutator'   =\u003e Awobaz\\Mutator\\Facades\\Mutator::class,\n```\n\n\u003e **Note:** **Eloquent Mutators** requires Laravel 5.4+.\n\nAfter installation, publish the assets using the `mutators:install` Artisan command. The primary configuration file will be located at `config/mutators.php`. The installation also publishes and registers the `app/Providers/MutatorServiceProvider.php`. Within this service provider, you may register custom accessors/mutators extensions.\n\n```sh\nphp artisan mutators:install\n```\n\n## Usage\n\n### Using the `Awobaz\\Mutator\\Database\\Eloquent\\Model` class\n\nSimply make your model class derive from the `Awobaz\\Mutator\\Database\\Eloquent\\Model` base class. The `Awobaz\\Mutator\\Database\\Eloquent\\Model` extends the `Eloquent` base class without changing its core functionality.\n\n### Using the `Awobaz\\Mutator\\Mutable` trait\n\nIf for some reasons you can't derive your models from `Awobaz\\Mutator\\Database\\Eloquent\\Model`, you may take advantage of the `Awobaz\\Mutator\\Mutable` trait. Simply use the trait in your models.\n\n### Syntax\n\nAfter configuring your model, you may configure accessors and mutators for its attributes.\n\n#### Defining accessors\n\nFor the following Post model, we configure accessors to trim whitespace from the beginning and end of the `title` and `content` attributes:\n\n```php\nnamespace App;\n\nuse Illuminate\\Database\\Eloquent\\Model;\n\nclass Post extends Model\n{\n    use \\Awobaz\\Mutator\\Mutable;\n    \n    protected $accessors = [\n        'title'   =\u003e 'trim_whitespace',\n        'content' =\u003e 'trim_whitespace',\n    ];\n}\n```\n\nAs you can see, we use an array property named `accessors` on the model to configure its **accessors**. Each key of the array represents the name of an attribute, and the value points to one or multiple accessors. To apply multiple accessors, pass an array as value (the accessors will be applied in the order they are specified):\n\n```php\nnamespace App;\n\nuse Illuminate\\Database\\Eloquent\\Model;\n\nclass Post extends Model\n{\n    use \\Awobaz\\Mutator\\Mutable;\n    \n    protected $accessors = [\n        'title'   =\u003e ['trim_whitespace', 'capitalize'], \n        'content' =\u003e ['trim_whitespace', 'remove_extra_whitespace'],\n    ];\n}\n```\n\n#### Defining mutators\n\nTo define mutators, use an array property named `mutators` instead.\n\n```php\nnamespace App;\n\nuse Illuminate\\Database\\Eloquent\\Model;\n\nclass Post extends Model\n{\n    use \\Awobaz\\Mutator\\Mutable;\n    \n    protected $mutators = [\n        'title'    =\u003e 'remove_extra_whitespace',\n    ];\n}\n```\n\n\u003e **Note:** The name of the properties used for accessors and mutators can be respectively configured in the `config/mutators.php` configuration file.\n\n### Defining accessors/mutators extensions\n\nIn the previous examples, we use [accessors/mutators provided](#built-in-accessorsmutators) by the package. You may also register accessors/mutators extensions using the **extend** method of the `Mutator` facade. The **extend** method accepts the name of the accessor/mutator and a closure.\n\n```\n\u003c?php\n\nnamespace App\\Providers;\n\nuse Awobaz\\Mutator\\Facades\\Mutator;\nuse Illuminate\\Support\\ServiceProvider;\n\nclass MutatorServiceProvider extends ServiceProvider\n{\n    /**\n     * Register any application services.\n     *\n     * @return void\n     */\n    public function register()\n    {\n        //Register your custom accessors/mutators extensions here.\n        Mutator::extend('extension_name', function($model, $value, $key){\n            //DO STUFF HERE AND RETURN THE VALUE\n        });\n    }\n}\n```\nAs you can see, the model ($model), the attribute's value ($value) and the attribute's name ($key) are passed to the closure, allowing you to access other attributes of the model to compute and return the desired value. \n\n#### Additional parameters\nYou can also define additional parameters for an extension. This give us the flexibility to implement dynamic accessors/mutators.\n\n```\n\u003c?php\n\nnamespace App\\Providers;\n\nuse Awobaz\\Mutator\\Facades\\Mutator;\nuse Illuminate\\Support\\ServiceProvider;\n\nclass MutatorServiceProvider extends ServiceProvider\n{\n    /**\n     * Register any application services.\n     *\n     * @return void\n     */\n    public function register()\n    {\n        //The following extension is an implementation for str_replace\n        Mutator::extend('str_replace', function ($model, $value, $key, $search, $replace) {\n            return str_replace($search, $replace, $value);\n        });\n    }\n}\n```\n\nIn the above example, the model ($model), the attribute's value ($value), the attribute's name ($key) and two additional parameters are passed to the closure.\n\nTo apply this extension, we can use the following syntaxes:\n\n```php\nnamespace App;\n\nuse Illuminate\\Database\\Eloquent\\Model;\n\nclass Post extends Model\n{\n    use \\Awobaz\\Mutator\\Mutable;\n    \n    protected $accessors = [\n        'content' =\u003e ['str_replace' =\u003e ['one', 'two']]\n        //OR \n        //'content' =\u003e 'str_replace:one,two'\n    ];\n}\n```\n\nThis will replace every occurence of ***one*** with ***two*** for the `content` attribute.\n\n## Built-in accessors/mutators\n\n- [`lower_case`](#lower_case)\n- [`upper_case`](#upper_case)\n- [`capitalize`](#capitalize)\n- [`capitalize_words`](#capitalize_words)\n- [`trim_whitespace`](#trim_whitespace)\n- [`camel_case`](#camel_case)\n- [`snake_case`](#snake_case)\n- [`kebab_case`](#kebab_case)\n- [`studly_case`](#studly_case)\n- [`title_case`](#title_case)\n- [`plural`](#plural)\n- [`singular`](#singular)\n- [`slug`](#slug)\n- [`remove_extra_whitespace`](#remove_extra_whitespace)\n- [`preg_replace:pattern,replacement[,limit]`](#preg_replacepatternreplacementlimit)\n\n### `lower_case`\nConvert the attribute to lower case.\n\n### `upper_case`\nConvert the attribute to upper case.\n\n### `capitalize`\nConvert the first character of attribute to upper case.\n\n### `capitalize_words`\nConvert the first character of each word of the attribute to upper case.\n\n### `trim_whitespace`\nStrip whitespace from the beginning and end of the attribute.\n\n### `camel_case`\nConvert the attribute to camel case.\n\n### `snake_case`\nConvert the attribute to snake case.\n\n### `studly_case`\nConvert the attribute to studly case.\n\n### `kebab_case`\nConvert the attribute to kebab case.\n\n### `title_case`\nConvert the attribute to title case.\n\n### `plural`\nConvert the attribute to its plural form (only supports the English language).\n\n### `singular`\nConvert the attribute to its singular form (only supports the English language).\n\n### `slug`\nConvert the attribute to its URL friendly \"slug\" form.\n\n### `remove_extra_whitespace`\nRemove extra whitespaces within the attribute.\n\n### `preg_replace:pattern,replacement[,limit]`\nPerform a regular expression search and replace on the attribute.\n\n## Versioning\n\nWe use [SemVer](http://semver.org/) for versioning. For the versions available, see the [tags on this repository](https://github.com/topclaudy/eloquent-mutators/tags).\n\n## Unit Tests\n\nIn order to run the test suite, install the development dependencies:\n\n```bash\n$ composer install --dev\n```\n\nThen, run the following command:\n\n```bash\n$ vendor/bin/phpunit\n```\n\n## Authors\n\n* [Claudin J. Daniel](https://github.com/topclaudy) - *Initial work*\n\n## Contributing\n\nPlease read [CONTRIBUTING.md](https://github.com/topclaudy/eloquent-mutators/blob/master/CONTRIBUTING.md) for details on our code of conduct, and the process for submitting pull requests.\n\n[![](https://sourcerer.io/fame/topclaudy/topclaudy/eloquent-mutators/images/0)](https://sourcerer.io/fame/topclaudy/topclaudy/eloquent-mutators/links/0)\n[![](https://sourcerer.io/fame/topclaudy/topclaudy/eloquent-mutators/images/1)](https://sourcerer.io/fame/topclaudy/topclaudy/eloquent-mutators/links/1)\n[![](https://sourcerer.io/fame/topclaudy/topclaudy/eloquent-mutators/images/2)](https://sourcerer.io/fame/topclaudy/topclaudy/eloquent-mutators/links/2)\n[![](https://sourcerer.io/fame/topclaudy/topclaudy/eloquent-mutators/images/3)](https://sourcerer.io/fame/topclaudy/topclaudy/eloquent-mutators/links/3)\n[![](https://sourcerer.io/fame/topclaudy/topclaudy/eloquent-mutators/images/4)](https://sourcerer.io/fame/topclaudy/topclaudy/eloquent-mutators/links/4)\n[![](https://sourcerer.io/fame/topclaudy/topclaudy/eloquent-mutators/images/5)](https://sourcerer.io/fame/topclaudy/topclaudy/eloquent-mutators/links/5)\n[![](https://sourcerer.io/fame/topclaudy/topclaudy/eloquent-mutators/images/6)](https://sourcerer.io/fame/topclaudy/topclaudy/eloquent-mutators/links/6)\n[![](https://sourcerer.io/fame/topclaudy/topclaudy/eloquent-mutators/images/7)](https://sourcerer.io/fame/topclaudy/topclaudy/eloquent-mutators/links/7)\n\n\n## Sponsored by\n\n* [Awobaz](https://awobaz.com) - Web/Mobile agency based in Montreal, Canada\n\n## License\n\n**Eloquent Mutators** is licensed under the [MIT License](http://opensource.org/licenses/MIT).","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftopclaudy%2Feloquent-mutators","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftopclaudy%2Feloquent-mutators","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftopclaudy%2Feloquent-mutators/lists"}