+ return $this->morphMany(Activity::class, 'entity')->orderBy('created_at', 'desc');
+ }
+
+ /**
+ * Get View objects for this entity.
+ */
+ public function views()
+ {
+ return $this->morphMany(View::class, 'viewable');
+ }
+
+ /**
+ * Get the Tag models that have been user assigned to this entity.
+ * @return \Illuminate\Database\Eloquent\Relations\MorphMany
+ */
+ public function tags()
+ {
+ return $this->morphMany(Tag::class, 'entity')->orderBy('order', 'asc');
+ }
+
+ /**
+ * Get the comments for an entity
+ * @param bool $orderByCreated
+ * @return MorphMany
+ */
+ public function comments($orderByCreated = true)
+ {
+ $query = $this->morphMany(Comment::class, 'entity');
+ return $orderByCreated ? $query->orderBy('created_at', 'asc') : $query;
+ }
+
+ /**
+ * Get the related search terms.
+ * @return \Illuminate\Database\Eloquent\Relations\MorphMany
+ */
+ public function searchTerms()
+ {
+ return $this->morphMany(SearchTerm::class, 'entity');
+ }
+
+ /**
+ * Get this entities restrictions.
+ */
+ public function permissions()
+ {
+ return $this->morphMany(EntityPermission::class, 'restrictable');
+ }
+
+ /**
+ * Check if this entity has a specific restriction set against it.
+ * @param $role_id
+ * @param $action
+ * @return bool
+ */
+ public function hasRestriction($role_id, $action)
+ {
+ return $this->permissions()->where('role_id', '=', $role_id)
+ ->where('action', '=', $action)->count() > 0;
+ }
+
+ /**
+ * Get the entity jointPermissions this is connected to.
+ * @return \Illuminate\Database\Eloquent\Relations\MorphMany
+ */
+ public function jointPermissions()
+ {
+ return $this->morphMany(JointPermission::class, 'entity');