__get() public method

Dynamically retrieve attributes.
public __get ( string $key ) : mixed
$key string
return mixed
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);
 }