Use as a Laravel cast
Example of a usage for this library as a Laravel cast on an Eloquent model.
Imagine we have a Laravel based project and one of our models stores bytes so we can convert them freely.
Create the cast using Laravel's command:
Now copy the following in your app\Casts\ByteUnit.php
file:
Next and finally you can add this cast class to any of your models that requires this on some of their attributes like so:
And voilá! You now have this cast to your model results on your API or any controller using toArray
or toJson
methods.
Check more documentation on this at the official Laravel documentation about Eloquent custom casts.