/** * Cast model attribute. Extend laravel attribute casting mutators by serialized array * @param string $key * @param mixed $value * @return mixed */ protected function castAttribute($key, $value) { if ($value === null) { return $value; } if ($this->getCastType($key) === 'serialize') { return $this->fromSerialize($value); } return parent::castAttribute($key, $value); }
/** * Cast an attribute to a native PHP type. * * @param string $key * @param mixed $value * @return mixed */ protected function castAttribute($key, $value) { if (is_null($value) || !$this->isExtensionCastable($key)) { return parent::castAttribute($key, $value); } switch ($this->getCastType($key)) { case 'base64': return base64_decode($value); default: return $value; } }
protected function castAttribute($key, $value) { if (is_null($value)) { return $value; } switch ($this->getCastType($key)) { case 'date_only': return (new \DateTime($value))->format('d/m/Y'); default: return parent::castAttribute($key, $value); } }
/** * Cast an attribute to a native PHP type. * * @param string $key * @param mixed $value * * @return mixed */ protected function castAttribute($key, $value) { //if cast type is json and the object already is decoded, don't try to re-decode if (in_array($this->getCastType($key), ['array', 'json', 'object']) && (is_array($value) || is_object($value))) { return $value; } // Run the parent cast rules in the parent method $value = parent::castAttribute($key, $value); if (is_null($value)) { return $value; } switch ($this->getCastType($key)) { case 'date': try { return $this->asDateTime($value); //otherwise try the alternatives } catch (\InvalidArgumentException $e) { return Carbon::createFromFormat('Y-m-d', $value); //if it is the true base ISO8601 date format, parse it } case 'datetime': return $this->asDateTime($value); //try the catchall method for date translation //try the catchall method for date translation default: return $value; } }
/** * Преобразование на чтение * * @param string $key * @param mixed $value * @return bool|mixed */ protected function castAttribute($key, $value) { if (is_null($value)) { return $value; } $types = explode('|', $this->getCastType($key)); foreach ($types as $type) { switch ($type) { case 'db_bool': $value = 'Y' == $value; break; case 'phone': if (11 == strlen($value)) { $value = sprintf('+%s (%s) %s-%s-%s', $value[0], substr($value, 1, 3), substr($value, 4, 3), substr($value, 7, 2), substr($value, -2)); } break; default: $value = parent::castAttribute($key, $value); } } return $value; }