]> BookStack Code Mirror - bookstack/blobdiff - app/Entity.php
Added view count tracking with personalised lists
[bookstack] / app / Entity.php
index 7aee4d3b082d81ca611cbdc493d74f33379d3061..1a8b02a4b1e7aee5e67706d843b74ce9e7b9d640 100644 (file)
@@ -6,6 +6,7 @@ use Illuminate\Database\Eloquent\Model;
 
 abstract class Entity extends Model
 {
+
     /**
      * Relation for the user that created this entity.
      * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
@@ -36,7 +37,7 @@ abstract class Entity extends Model
     }
 
     /**
-     * Gets the activity for this entity.
+     * Gets the activity objects for this entity.
      * @return \Illuminate\Database\Eloquent\Relations\MorphMany
      */
     public function activity()
@@ -44,6 +45,24 @@ abstract class Entity extends Model
         return $this->morphMany('BookStack\Activity', 'entity')->orderBy('created_at', 'desc');
     }
 
+    /**
+     * Get View objects for this entity.
+     * @return mixed
+     */
+    public function views()
+    {
+        return $this->morphMany('BookStack\View', 'viewable');
+    }
+
+    /**
+     * Get just the views for the current user.
+     * @return mixed
+     */
+    public function userViews()
+    {
+        return $this->views()->where('user_id', '=', auth()->user()->id);
+    }
+
     /**
      * Allows checking of the exact class, Used to check entity type.
      * Cleaner method for is_a.