Json.php 674 B

1234567891011121314151617181920212223242526272829
  1. <?php
  2. namespace App\Casts;
  3. use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
  4. use Illuminate\Database\Eloquent\Model;
  5. class Json implements CastsAttributes
  6. {
  7. /**
  8. * Cast the given value.
  9. *
  10. * @param array<string, mixed> $attributes
  11. */
  12. public function get(Model $model, string $key, mixed $value, array $attributes): mixed
  13. {
  14. return json_decode($value, true);
  15. }
  16. /**
  17. * Prepare the given value for storage.
  18. *
  19. * @param array<string, mixed> $attributes
  20. */
  21. public function set(Model $model, string $key, mixed $value, array $attributes): mixed
  22. {
  23. return json_encode($value);
  24. }
  25. }