Esensi\Model\Model::__get PHP Метод

__get() публичный Метод

Dynamically retrieve attributes.
public __get ( string $key ) : mixed
$key string
Результат mixed
    public function __get($key)
    {
        // Resolve relationship dynamically
        if ($relationship = $this->getDynamicRelationship($key)) {
            return $relationship;
        }
        // Dynamically retrieve the encryptable attribute
        $value = $this->getDynamicEncrypted($key);
        if (is_null($value) || !is_string($value)) {
            // Fallback to the default Eloquent dynamic getter
            $value = parent::__get($key);
        }
        // Dynamically juggle the attribute.
        // This is always called so that even decrypted values
        // can be casted after decrypting.
        return $this->getDynamicJuggle($key, $value);
    }

Usage Example

Пример #1
0
 /**
  * Dynamically retrieve attributes.
  *
  * @param  string $key
  * @return mixed
  */
 public function __get($key)
 {
     // Dynamically get time since attributes
     $normalized = Str::snake($key);
     $attribute = str_replace(['time_since_', 'time_till_'], ['', ''], $normalized);
     if ((Str::startsWith($normalized, 'time_since_') || Str::startsWith($normalized, 'time_till_')) && in_array($attribute . '_at', $this->getDates())) {
         // Convert the attribute to a Carbon date
         $value = $this->getAttributeFromArray($attribute . '_at');
         // Show label if date has not been set
         if (is_null($value)) {
             return Lang::get('esensi/core::core.labels.never_' . $attribute);
         }
         // Show human readable date
         $date = $this->asDateTime($value);
         return $date->diffForHumans();
     }
     // Default dynamic getter
     return parent::__get($key);
 }