/** * Perform the action * @param Model $model * @param type $event * @return HistoryLog|null Returns null when nothing changes */ public function performAction(Model $model, $event) { $modelPrevState = $model->getPrevState(); $changedValues = $model->getModifiedAttributes(); if (count($changedValues)) { $history = new HistoryLog(); $history->version = $modelPrevState ? $modelPrevState->version + 1 : 0; $history->model_type = get_class($model); $history->model_id = $model->id; $history->user_id = $model->getUserId(); $history->changed_value = json_encode($changedValues); $history->save(); return $history; } return null; }
/** * Get the Model history states * @param integer $limit * @param integer $offset * @return Collection | null Collection on success */ public function getStates($limit = 1, $offset = 0, $orderDirection = 'desc') { return HistoryLog::where('model_id', $this->id)->where('model_type', get_class($this))->orderBy('created_at', $orderDirection)->offset($offset)->limit($limit)->get(); }