3 namespace BookStack\Actions;
5 use BookStack\Interfaces\Viewable;
7 use Illuminate\Database\Eloquent\Relations\MorphTo;
11 * Views are stored per-item per-person within the database.
12 * They can be used to find popular items or recently viewed items
13 * at a per-person level. They do not record every view instance as an
14 * activity. Only the latest and original view times could be recognised.
16 * @property int $views
17 * @property int $user_id
19 class View extends Model
21 protected $fillable = ['user_id', 'views'];
24 * Get all owning viewable models.
26 public function viewable(): MorphTo
28 return $this->morphTo();
32 * Increment the current user's view count for the given viewable model.
34 public static function incrementFor(Viewable $viewable): int
37 if (is_null($user) || $user->isDefault()) {
41 /** @var View $view */
42 $view = $viewable->views()->firstOrNew([
43 'user_id' => $user->id,
46 $view->forceFill(['views' => $view->views + 1])->save();
52 * Clear all views from the system.
54 public static function clearAll()
56 static::query()->truncate();