So, I finished the βTraitsβ route to redefine the various methods of the model; this, it turns out, is not for the faint of heart, since casting attributes is rather deeply embedded in the way models work.
To make this work for the most general case, that is, to be able to easily add custom casts, a fairly serious revision of the model will be required.
Here is the trait I wrote to add time:
<?php namespace App\Models; use Carbon\Carbon; trait CustomCasts { protected function castAttribute($key, $value) { if (is_null($value)) { return $value; } switch ($this->getCastType($key)) { case 'int': case 'integer': return (int) $value; case 'real': case 'float': case 'double': return (float) $value; case 'string': return (string) $value; case 'bool': case 'boolean': return (bool) $value; case 'object': return $this->fromJson($value, true); case 'array': case 'json': return $this->fromJson($value); case 'collection': return new BaseCollection($this->fromJson($value)); case 'date': case 'datetime': return $this->asDateTime($value); case 'timestamp': return $this->asTimeStamp($value); case 'time': return $this->asTime($value); default: return $value; } } protected function asTime($value) {
source share