public function updated(Model $model) { if ($model->isDirty('parent_id')) { /** * 同步Original数据,是為了清除parent_id的dirty狀態 */ $model->syncOriginal(); $tableName = $model->getTable(); $oldNode = $model->getOriginal('node'); if (0 < $model->parent_id) { /** * @var Model $parent */ $parent = $model->find($model->parent_id); $model->node = $parent->node . $model->id . self::SEPARATOR; $model->save(); //取出原来的level $originalLevel = Cache::pull('node-category-original-level-' . $model->id); //计算新level与原来的level的差值 $i = $model->level - $originalLevel; DB::table($tableName)->where('node', 'like', $oldNode . '%')->where('id', '<>', $model->id)->update(['level' => DB::raw("level + {$i}"), 'node' => DB::raw("REPLACE(`node`, '{$oldNode}', '{$model->node}')")]); } else { //修改為頂級分類 $model->level = 1; $model->node = self::SEPARATOR . $model->id . self::SEPARATOR; $model->save(); DB::table($tableName)->where('node', 'like', $oldNode . '%')->where('id', '<>', $model->id)->update(['level' => DB::raw("level - {$model->level}"), 'node' => DB::raw("CONCAT('{$model->node}', `id`, ',')")]); } } }
/** * Add the slug when not explicitly stated. * * @param Model $model * @return void */ public function saving(Model $model) { $hasManuallyAssignedSlug = $model->slug && $model->isDirty('slug'); if (!$model->isDirty('name') || $hasManuallyAssignedSlug) { return; } $slug = str_slug($model->getAttribute($this->fieldToSlug)); $model->setAttribute($this->slugAttribute, $slug); }
/** * 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); } }
/** * Checks if the translation is dirty * * @param Eloquent $translation * @return bool */ protected function isTranslationDirty(Eloquent $translation) { return $translation->isDirty(); }
/** * 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; } $this->deleteFile($fieldname, $model); } }
/** * The function that builds the slug. * * The slug field takes precedence over the name. * * @param Model $model - the Slug model * * @return string */ public function buildsSlug(Model $model) { if ($model->isDirty($model->getSlugColumnKey())) { $column = $model->getSlugColumnKey(); } else { $column = $model->getNameColumnKey(); } return $this->slugify->slugify($model->getAttribute($column), array_get($this->config, 'slug_separator')); }
/** * Determine if the model or given attribute(s) have been modified. * * @param array|string|null $attributes * @return bool */ public function isDirty($attributes = null) { return parent::isDirty($attributes) or $this->getSource()->isDirty($attributes); }
protected function checkDirty(Model $model) { if ($model->isDirty()) { $this->changed++; } }
/** * Update the creation and update timestamps. * * @return void */ protected function updateTimestamps(Model $model) { // Check if this model uses timestamps first. if (!$model->timestamps) { return; } $time = $model->freshTimestamp(); if (!$model->isDirty(Model::UPDATED_AT)) { $model->setUpdatedAt($time); } if (!$model->exists && !$model->isDirty(Model::CREATED_AT)) { $model->setCreatedAt($time); } }