Inheritance: extends Illuminate\Database\Eloquent\Model
Example #1
0
 /**
  * Diff the attributes of this version model against another version.
  * If no version is provided, it will be diffed against the current version.
  *
  * @param Version|null $againstVersion
  * @return array
  */
 public function diff(Version $againstVersion = null)
 {
     $model = $this->getModel();
     $diff = $againstVersion ? $againstVersion->getModel() : $this->versionable()->withTrashed()->first()->currentVersion()->getModel();
     $diffArray = array_diff_assoc($diff->getAttributes(), $model->getAttributes());
     if (isset($diffArray[$model->getCreatedAtColumn()])) {
         unset($diffArray[$model->getCreatedAtColumn()]);
     }
     if (isset($diffArray[$model->getUpdatedAtColumn()])) {
         unset($diffArray[$model->getUpdatedAtColumn()]);
     }
     if (method_exists($model, 'getDeletedAtColumn') && isset($diffArray[$model->getDeletedAtColumn()])) {
         unset($diffArray[$model->getDeletedAtColumn()]);
     }
     return $diffArray;
 }
 /**
  * Save a new version.
  * @return void
  */
 protected function versionablePostSave()
 {
     /**
      * We'll save new versions on updating and first creation
      */
     if ($this->versioningEnabled === true && $this->updating && $this->isValidForVersioning() || $this->versioningEnabled === true && !$this->updating) {
         // Save a new version
         $version = new Version();
         $version->versionable_id = $this->getKey();
         $version->versionable_type = get_class($this);
         $version->user_id = $this->getAuthUserId();
         $version->model_data = serialize($this->getAttributes());
         if (!empty($this->reason)) {
             $version->reason = $this->reason;
         }
         $version->save();
     }
 }