public dash and view counting
This commit is contained in:
+30
-2
@@ -2,11 +2,10 @@
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Models\Concerns\ManyManyThrough;
|
||||
use App\Models\Concerns\Reportable;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Collection;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Riesjart\Relaquent\Model\Concerns\HasRelaquentRelationships;
|
||||
|
||||
/**
|
||||
* A data table object (referencing revisions)
|
||||
@@ -22,6 +21,7 @@ use Riesjart\Relaquent\Model\Concerns\HasRelaquentRelationships;
|
||||
* @property string $description
|
||||
* @property string $license
|
||||
* @property string $origin
|
||||
* @property int $visits
|
||||
* @property-read string $viewPage
|
||||
* @property-read User $owner
|
||||
* @property-read Table $parentTable
|
||||
@@ -38,6 +38,21 @@ class Table extends Model
|
||||
use Reportable;
|
||||
protected $guarded = [];
|
||||
|
||||
protected $touches = ['owner'];
|
||||
|
||||
public function countVisit()
|
||||
{
|
||||
// Temporarily disable timestamps to avoid touching updated_at
|
||||
$oldt = $this->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()
|
||||
{
|
||||
@@ -92,6 +107,14 @@ class Table extends Model
|
||||
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()
|
||||
{
|
||||
@@ -106,4 +129,9 @@ class Table extends Model
|
||||
|
||||
return parent::__get($name);
|
||||
}
|
||||
|
||||
public function scopeForList(Builder $query)
|
||||
{
|
||||
return $query->with('revision:id,row_count')->withCount(['favourites', 'forks', 'revisions']);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user