4 use Illuminate\Support\Facades\DB;
6 class Comment extends Ownable
8 protected $fillable = ['text', 'html'];
11 * Get the entity that this comment belongs to
12 * @return \Illuminate\Database\Eloquent\Relations\MorphTo
14 public function entity()
16 return $this->morphTo('entity');
20 * Get the page that this comment is in.
21 * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
23 public function page()
25 return $this->belongsTo(Page::class);
29 * Get the owner of this comment.
30 * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
32 public function user()
34 return $this->belongsTo(User::class);
37 public function getParentCommentsByPage($pageId, $pageNum = 0, $limit = 0) {
38 $data = ['pageId' => $pageId];
39 $query = static::newQuery();
40 $query->join('users AS u', 'comments.created_by', '=', 'u.id');
41 $query->leftJoin('users AS u1', 'comments.updated_by', '=', 'u1.id');
42 $query->leftJoin('images AS i', 'i.id', '=', 'u.image_id');
43 $query->selectRaw('comments.id, text, html, comments.created_by, comments.updated_by, comments.created_at, comments.updated_at, '
44 . 'u.name AS created_by_name, u1.name AS updated_by_name, '
45 . '(SELECT count(c.id) FROM bookstack.comments c WHERE c.parent_id = comments.id AND page_id = ?) AS cnt_sub_comments, i.url AS avatar ',
47 $query->whereRaw('page_id = ? AND parent_id IS NULL', [$pageId]);
48 $query->orderBy('created_at');