1 <?php namespace BookStack\Actions;
3 use BookStack\Interfaces\Viewable;
5 use Illuminate\Database\Eloquent\Relations\MorphTo;
9 * Views are stored per-item per-person within the database.
10 * They can be used to find popular items or recently viewed items
11 * at a per-person level. They do not record every view instance as an
12 * activity. Only the latest and original view times could be recognised.
14 * @property int $views
15 * @property int $user_id
17 class View extends Model
20 protected $fillable = ['user_id', 'views'];
23 * Get all owning viewable models.
25 public function viewable(): MorphTo
27 return $this->morphTo();
31 * Increment the current user's view count for the given viewable model.
33 public static function incrementFor(Viewable $viewable): int
36 if (is_null($user) || $user->isDefault()) {
40 /** @var View $view */
41 $view = $viewable->views()->firstOrNew([
42 'user_id' => $user->id,
45 $view->forceFill(['views' => $view->views + 1])->save();
51 * Clear all views from the system.
53 public static function clearAll()
55 static::query()->truncate();