/** * creates an activity entry for each dirty attribute * @param \Illuminate\Database\Eloquent\Model $model */ public function updated($model) { $dirty = $model->getDirty(); foreach ($dirty as $key => $value) { $data = ['type' => $key, 'note' => $value]; $model->activity()->create($data); } }
public function save(Model $model) { if ($model->getDirty()) { return $model->save(); } else { return $model->touch(); } }
/** * Records differences between dirty and * clean attributes to the database. * * @param Illuminate\Database\Eloquent\Model $model * @return boolean Whether there was a Changeset created */ public function record(Model $model) { $dirty = $model->getDirty(); // Remove dirty keys not in the auditable array if (isset($model->auditable) && is_array($model->auditable) && !in_array('*', $model->auditable)) { $dirty = array_intersect_key($dirty, array_flip($model->auditable)); } if ($model->exists && count($dirty) > 0) { // Grab the models PK $primarykey = isset($model->primarykey) ? $model->primarykey : 'id'; /* * ------------------------------------------------- * Create the Changeset * ------------------------------------------------- * We have to improvise a little here as we * don't want to depend on models having * relationships with the Auditable Changeset model. */ $cs = new Changeset(); $cs->object_type = get_class($model); // Manual $cs->object_id = $model->{$primarykey}; // Manual $cs->user_id = \Auth::check() ? \Auth::user()->id : 0; $cs->save(); // Add the Changeset changes foreach ($dirty as $k => $v) { $change = new Change(); $change->key = $k; $change->old_value = $model->getOriginal($k); $change->new_value = $v; $cs->changes()->save($change); } return true; } return false; }
/** * @param \Illuminate\Database\Eloquent\Model $translation * * @return bool */ protected function isTranslationDirty(Model $translation) { $dirtyAttributes = $translation->getDirty(); unset($dirtyAttributes[$this->getLocaleKey()]); return count($dirtyAttributes) > 0; }
/** * @override * Get the attributes that have been changed since last sync. * * @return array */ public function getDirty() { $dirty = parent::getDirty(); // We need to remove the primary key from the dirty attributes since primary keys // never change and when updating it shouldn't be part of the attributes. if (isset($dirty[$this->primaryKey])) { unset($dirty[$this->primaryKey]); } return $dirty; }
/** * Sets the proper data attributes and rules arrays depending on whether or not the model exists * * @param \Illuminate\Database\Eloquent\Model $model * * @return array //'data' and 'rules' indexes both arrays */ public function prepareDataAndRules($model) { //fetch the rules if any exist $rules = $this->getModelValidationRules(); //if the model exists, this is an update if ($model->exists) { //only include dirty fields $data = $model->getDirty(); //and validate the fields that are being updated $rules = array_intersect_key($rules, $data); } else { //otherwise validate everything $data = $model->getAttributes(); } return compact('data', 'rules'); }
/** * Store/save current state of the model * * @param \Illuminate\Database\Eloquent\Model $model * @return mixed */ protected function storeEloquentModel($model) { if ($model->getDirty()) { return $model->save(); } else { return $model->touch(); } }
/** * @return array */ public function getDirty() { $dirty = parent::getDirty(); $dirty['settings'] = json_encode($this->{$this->getSettingsProperty()}); return $dirty; }
/** * Get the model's "after" snapshot. * * @return array */ protected function getAfter() { $lookupTable = ['created' => $this->model->getAttributes(), 'updated' => $this->model->getDirty(), 'deleted' => null, 'soft-deleted' => ['deleted_at' => $this->model->{$this->model->getDeletedAtColumn()}], 'restored' => ['deleted_at' => $this->model->{$this->model->getDeletedAtColumn()}]]; return collect($lookupTable)->get($this->getEvent()); }
/** * Override is dirty so we can trigger update if we have dirty images. * @return array */ public function getDirty() { $dirty = parent::getDirty(); if (!empty($this->files)) { // We just set the ID to the same value to trigger the update. $dirty[$this->getKeyName()] = $this->getKey(); } return $dirty; }
/** * @param \Illuminate\Database\Eloquent\Model $translation * * @return bool */ protected function isTranslationDirty(Model $translation) { $dirtyAttributes = $translation->getDirty(); return count($dirtyAttributes) > 0; }