Beispiel #1
0
 /**
  * Set a given attribute on the model.
  * @param string $key
  * @param mixed $value
  * @return void
  */
 public function setAttribute($key, $value)
 {
     // Before Event
     if (($_value = $this->fireEvent('model.beforeSetAttribute', [$key, $value], true)) !== null) {
         $value = $_value;
     }
     // Handle jsonable
     if (in_array($key, $this->jsonable) && (!empty($value) || is_array($value))) {
         $value = json_encode(Arr::fixKeysNumber($value));
     }
     // Handle direct relation setting
     if ($this->hasRelation($key)) {
         $result = $this->setRelationValue($key, $value);
     } else {
         if (!is_object($value) && !is_array($value) && !is_null($value) && !is_bool($value)) {
             $value = trim($value);
         }
         $result = parent::setAttribute($key, $value);
     }
     // After Event
     $this->fireEvent('model.setAttribute', [$key, $value]);
     return $result;
 }
Beispiel #2
0
 /**
  * Locates relations with delete flag and cascades the delete event.
  * @return void
  */
 protected function performDeleteOnRelations()
 {
     $definitions = $this->getRelationDefinitions();
     foreach ($definitions as $type => $relations) {
         /*
          * Hard 'delete' definition
          */
         foreach ($relations as $name => $options) {
             if (!Arr::get($options, 'delete', false)) {
                 continue;
             }
             if (!($relation = $this->{$name})) {
                 continue;
             }
             if ($relation instanceof EloquentModel) {
                 $relation->forceDelete();
             } elseif ($relation instanceof CollectionBase) {
                 $relation->each(function ($model) {
                     $model->forceDelete();
                 });
             }
         }
         /*
          * Belongs-To-Many should clean up after itself always
          */
         if ($type == 'belongsToMany') {
             foreach ($relations as $name => $options) {
                 $this->{$name}()->detach();
             }
         }
     }
 }
Beispiel #3
0
 /**
  * Get the model's original attribute values.
  *
  * @param  string|null  $key
  * @param  mixed  $default
  * @return array
  */
 public function getOriginal($key = null, $default = null)
 {
     return Arr::get($this->original, $key, $default);
 }