As a follow up to the previous entry, you can also include a function to return a casts array for a trait/concern that your eloquent model is using.
trait HasUuid
{
public function getHasUuidCasts()
{
if(config('uuid.binary_format')) {
return ['uuid' => 'uuid'];
}
return [];
}
//
}
Here is the trait that will enable this functionality on your eloquent models.
trait HasTraitsWithCasts
{
/**
* Get the casts array.
*
* @return array
*/
public function getCasts()
{
$class = static::class;
foreach (class_uses_recursive($class) as $trait) {
$method = 'get'.class_basename($trait).'Casts');
if (method_exists($class, $method) {
$this->casts = array_unique(
array_merge($this->casts, $this->{$method}())
);
}
}
return parent::getCasts();
}
}