/** * Update the cache for all operations. * * @param Model $model */ public function updateCache(Model $model) { $this->model = $model; $this->applyToCountCache(function ($config) { $foreignKey = $this->key($this->model, $config['foreignKey']); if ($this->model->getOriginal($foreignKey) && $this->model->{$foreignKey} != $this->model->getOriginal($foreignKey)) { $this->update($config, '-', $this->model->getOriginal($foreignKey)); $this->update($config, '+', $this->model->{$foreignKey}); } }); }
/** * On update, delete previous file if changed * * @param Model $model eloquent * @return mixed false or void */ public function updated(Model $model) { if (!($attachments = $model->attachments)) { return; } foreach ($attachments as $fieldname) { // Nothing to do if file did not change if ($model->getOriginal($fieldname) == $model->{$fieldname}) { return; } Croppa::delete('/uploads/' . $model->getTable() . '/' . $model->getOriginal($fieldname)); } }
/** * Observe model saving for User. * * @param \Illuminate\Database\Eloquent\Model $model * * @return void */ public function updating(Model $model) { $changes = []; $watchlist = (array) Arr::get($this->config, 'watchlist', []); foreach ($watchlist as $attribute) { if ($model->isDirty($attribute)) { $changes[$attribute] = $model->getAttribute($attribute); } } $recipient = new GenericRecipient($model->getOriginal('email'), $model->getRecipientName()); $this->factory->notify($recipient, $model, $changes, $this->config); }
/** * On update, delete previous file if changed * * @param Model $model eloquent * @return mixed false or void */ public function updated(Model $model) { if (!($attachments = $model->attachments)) { return; } foreach ($attachments as $fieldname) { // Nothing to do if file did not change if (!$model->isDirty($fieldname)) { continue; } $file = '/uploads/' . $model->getTable() . '/' . $model->getOriginal($fieldname); $this->delete($file); } }
/** * On save, upload files. * * @param Model $model eloquent * * @return mixed false or void */ public function saving(Model $model) { if (!($attachments = $model->attachments)) { return; } foreach ($attachments as $fieldname) { if (Request::hasFile($fieldname)) { // delete prev image $file = FileUpload::handle(Request::file($fieldname), 'uploads/' . $model->getTable()); $model->{$fieldname} = $file['filename']; if ($model->getTable() == 'files') { $model->fill($file); } } else { if ($model->{$fieldname} == 'delete') { $model->{$fieldname} = null; } else { $model->{$fieldname} = $model->getOriginal($fieldname); } } } }
public function updating(\Illuminate\Database\Eloquent\Model $model) { // An node is being updated. // Rebuild the tree if the parent was changed. // Get column names. // Missing required columns will throw an exception. $pkColumn = $model->getKeyName(); $leftColumn = $model->getLeftColumn(); $rightColumn = $model->getRightColumn(); $parentColumn = $model->getParentColumn(); $depthColumn = $model->getDepthColumn(); $groupColumn = $model->getGroupColumn(); $originalParentId = $model->getOriginal($parentColumn); if ($originalParentId !== $model->{$parentColumn}) { $model->syncOriginalAttribute($parentColumn); echo 'parent changed from "' . $originalParentId . '" to "' . $model->{$parentColumn} . '"'; if (!is_null($originalParentId)) { // Load original parent $originalParent = $model::findOrFail($originalParentId, array_filter([$pkColumn, $leftColumn, $rightColumn, $depthColumn, $groupColumn])); } if (!is_null($model->{$parentColumn})) { // Load new parent $newParent = $model::findOrFail($model->{$parentColumn}, array_filter([$pkColumn, $leftColumn, $rightColumn, $depthColumn, $groupColumn])); if ($newParent->{$leftColumn} > $model->{$leftColumn} and $newParent->{$leftColumn} < $model->{$rightColumn}) { // This change would create a circular tree. throw new \Exception('Changing this node\'s parent to ' . $model->{$parentColumn} . ' creates a circular reference'); } } //$originalParent = $model->where // @TODO: Move this to the Trait. Override save(). dd($modelId); /* SELECT id FROM node WHERE node.left BETWEEN 1 AND 10 AND node.id = 8 */ // Rebuild the entire tree. // @TODO: Be smarter about this method. dd($model); $rootNode = $model::where($parentColumn, '=', null)->first(); $rootNode->buildTree(); } return true; }
/** * Processes a recursive rollback operation. * * @param mixed $movement * * @return array */ protected function processRecursiveRollbackOperation(Model $movement) { /* * Retrieve movements that were created after * the specified movement, and order them descending */ $movements = $this->movements()->where('created_at', '>=', $movement->getOriginal('created_at'))->orderBy('created_at', 'DESC')->get(); $rollbacks = []; if ($movements->count() > 0) { foreach ($movements as $movement) { $rollbacks = $this->processRollbackOperation($movement); } } return $rollbacks; }
/** * Handle the case where no file has been uploaded for a field, but the field may have been cleared. * * @param Model $model * @param string $fieldName */ private function noUpload(Model $model, $fieldName) { $prevFilePath = $model->getOriginal($fieldName); if (!exists($model->{$fieldName}) && exists($prevFilePath)) { $this->deleteFile($prevFilePath); } }
/** * @param $attribute * @return array|mixed */ public function getDefaultValueFor($attribute) { return $this->model->getOriginal($attribute); }
/** * Get the model's "before" snapshot. * * @return array */ protected function getBefore() { $lookupTable = ['created' => null, 'updated' => array_intersect_key($this->model->getOriginal(), $this->getAfter()), 'deleted' => $this->model->getAttributes(), 'soft-deleted' => ['deleted_at' => $this->model->{$this->model->getDeletedAtColumn()}], 'restored' => ['deleted_at' => $this->model->{$this->model->getDeletedAtColumn()}]]; return collect($lookupTable)->get($this->getEvent()); }
public function restored(Eloquent $object) { $object->logs()->create(['type' => Model::RESTORE, 'data' => $object->getOriginal()]); }
public function deleted(Model $model) { $oldNode = $model->getOriginal('node'); DB::table($model->getTable())->where(DB::raw("LOCATE('{$oldNode}', `node`)"), '>', 0)->delete(); }
/** * Update the cache for all operations. */ public function update() { $this->apply(function ($config) { $foreignKey = $this->key($config['foreignKey']); if ($this->model->getOriginal($foreignKey) && $this->model->{$foreignKey} != $this->model->getOriginal($foreignKey)) { $this->updateCacheRecord($config, '-', 1, $this->model->getOriginal($foreignKey)); $this->updateCacheRecord($config, '+', 1, $this->model->{$foreignKey}); } }); }