Last active
July 8, 2021 08:46
-
-
Save dividy/e5185c098ed8bc6bb518f6239f1f08f0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Casts; | |
use Illuminate\Contracts\Database\Eloquent\CastsAttributes; | |
class ReadableNumber implements CastsAttributes | |
{ | |
/** | |
* Prepare the given value for storage. | |
* | |
* @param \Illuminate\Database\Eloquent\Model $model | |
* @param string $key | |
* @param array $value | |
* @param array $attributes | |
* @return string | |
*/ | |
public function get($model, $key, $value, $attributes) | |
{ | |
return (isset($value) ? number_format($value, 2, ',', ' ') : null); | |
} | |
public function set($model, $key, $value, $attributes) | |
{ | |
return [ $key => str_replace(" ", "", str_replace(",", ".", $value)) ]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage in model:
In your migration :