protected static function boot() { parent::boot(); static::creating(function ($entity) { if (!Schema::hasCollection('entities')) { static::createSchema(); } if (!empty($entity->hash)) { if (Entity::withTrashed()->where('hash', $entity->hash)->first()) { throw new Exception("Hash already exists for: " . $entity->title); } } if (empty($entity->_id)) { $entity->_id = static::generateIncrementedBaseURI($entity); } if (Auth::check()) { $entity->user_id = Auth::user()->_id; } else { $entity->user_id = "crowdwatson"; } }); static::saving(function ($entity) { if (Auth::check()) { $entity->user_id = Auth::user()->_id; } else { $entity->user_id = "crowdwatson"; } static::validateEntity($entity); }); static::saved(function ($entity) { \MongoDB\Temp::truncate(); Cache::flush(); }); static::deleted(function ($entity) { Cache::flush(); }); }