Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/grummfy/eloquent-extended-cast-model
This library help you to make more advanced cast to eloquent model.
https://github.com/grummfy/eloquent-extended-cast-model
cast eloquent laravel trait
Last synced: about 1 month ago
JSON representation
This library help you to make more advanced cast to eloquent model.
- Host: GitHub
- URL: https://github.com/grummfy/eloquent-extended-cast-model
- Owner: Grummfy
- License: mit
- Created: 2018-01-28T19:14:20.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2018-11-29T14:02:54.000Z (almost 6 years ago)
- Last Synced: 2024-05-01T19:23:25.895Z (7 months ago)
- Topics: cast, eloquent, laravel, trait
- Language: PHP
- Size: 11.7 KB
- Stars: 5
- Watchers: 2
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# Laravel extended cast model
This library help you to make more advanced cast to eloquent model.
Compatible with laravel 5.4 and upper.
Tested with laravel 5.4, 5.5, 5.6 and 5.7.
## Install
```
composer require grummfy/eloquent-extended-cast-model
```## Usage
Using the trait to facilitate the usage of custom cast on model. There is two traits:
* CastableModel: allow you to use custom cast
* JsonCollectionCastable: allow you to use json collection on castSee [example](example) directory for a possible usage.
### Custom casting in your model
* add the trait `CastableModel`
* in the $cast property of the model, add your field and the custom cast `'fooField' => 'bar'`
* add two methods: `toBar` and `fromBar` (Bar is the same name as the one present in the $cast property)If you want to add some extra value to your cast method (like it's done in the JsonReadOnlyCollection), you can fill the property
`$castParameters` of your model: `'fooField' => ['argument1', 'argument2']`.### Cast JsonReadOnlyCollection
This library came with a json collection. It's a collection of value that are saved in json. It's nice to use with a
collection of value object.## TODO
* unit test
* QA tools
* travis
* styleci
* scrutinizer
* ...## Note
The basic idea was inspired from https://github.com/reliese/laravel/blob/master/src/Database/Eloquent/Model.php