touches; $this->touches = []; $this->timestamps = false; $this->increment('visits'); $this->timestamps = true; $this->touches = $oldt; } /** Get owner from the instance cache (use when building table lists) */ public function cachedOwner() { return User::getCached($this->owner_id); } /** Owning user */ public function owner() { return $this->belongsTo(User::class, 'owner_id'); } /** Fork precursor */ public function parentTable() { return $this->belongsTo(Table::class, 'ancestor_id'); } /** Fork descendants */ public function forks() { return $this->hasMany(Table::class, 'ancestor_id'); } /** All related revisions (this may include merged revisions) */ public function revisions() { return $this->belongsToMany(Revision::class, 'table_revision_pivot'); } /** Active revision */ public function revision() { return $this->belongsTo(Revision::class, 'revision_id'); } /** Proposals submitted to this table */ public function proposals() { return $this->hasMany(Proposal::class); } /** User-submitted comments */ public function comments() { return $this->hasMany(TableComment::class); } /** Users favouriting this table */ public function favouritingUsers() { return $this->belongsToMany(User::class, 'table_favourites'); } /** * Relation to the pivot table, for favourite counting */ public function favourites() { return $this->hasMany(TableFavouritePivot::class); } /** Users to notify about comments */ public function discussionFollowers() { return $this->belongsToMany(User::class, 'discussion_follows'); } public function __get($name) { if (ends_with($name, 'Route')) { $arg = [ 'user' => $this->cachedOwner()->name, 'table' => $this->name ]; switch ($name) { case 'viewRoute': return route('table.view', $arg); case 'settingsRoute': return route('table.conf', $arg); case 'draftRoute': return route('table.draft', $arg); case 'deleteRoute': return route('table.delete', $arg); case 'draftDiscardRoute': return route('table.draft-discard', $arg); case 'draftUpdateRoute': return route('table.draft-update', $arg); } } if ($name == 'draftSessionKey') return "proposal.{$this->id}"; return parent::__get($name); } public function getDraftRoute($tab=null) { return route('table.draft', [ 'user' => $this->cachedOwner()->name, 'table' => $this->name, 'tab' => $tab, ]); } public function scopeForList(Builder $query) { return $query->with('revision:id,row_count')->with('owner:id,name,title') ->withCount(['favourites', 'forks', 'revisions', 'proposals']); } }