5 class Comment extends Ownable
7 public $sub_comments = [];
8 protected $fillable = ['text', 'html', 'parent_id'];
9 protected $appends = ['created', 'updated', 'sub_comments'];
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);
38 * Not being used, but left here because might be used in the future for performance reasons.
40 public function getPageComments($pageId) {
41 $query = static::newQuery();
42 $query->join('users AS u', 'comments.created_by', '=', 'u.id');
43 $query->leftJoin('users AS u1', 'comments.updated_by', '=', 'u1.id');
44 $query->leftJoin('images AS i', 'i.id', '=', 'u.image_id');
45 $query->selectRaw('comments.id, text, html, comments.created_by, comments.updated_by, '
46 . 'comments.created_at, comments.updated_at, comments.parent_id, '
47 . 'u.name AS created_by_name, u1.name AS updated_by_name, '
48 . 'i.url AS avatar ');
49 $query->whereRaw('page_id = ?', [$pageId]);
50 $query->orderBy('created_at');
54 public function getAllPageComments($pageId) {
55 return self::where('page_id', '=', $pageId)->with(['createdBy' => function($query) {
56 $query->select('id', 'name', 'image_id');
57 }, 'updatedBy' => function($query) {
58 $query->select('id', 'name');
59 }, 'createdBy.avatar' => function ($query) {
60 $query->select('id', 'path', 'url');
64 public function getCommentById($commentId) {
65 return self::where('id', '=', $commentId)->with(['createdBy' => function($query) {
66 $query->select('id', 'name', 'image_id');
67 }, 'updatedBy' => function($query) {
68 $query->select('id', 'name');
69 }, 'createdBy.avatar' => function ($query) {
70 $query->select('id', 'path', 'url');
74 public function getCreatedAttribute() {
76 'day_time_str' => $this->created_at->toDayDateTimeString(),
77 'diff' => $this->created_at->diffForHumans()
82 public function getUpdatedAttribute() {
83 if (empty($this->updated_at)) {
87 'day_time_str' => $this->updated_at->toDayDateTimeString(),
88 'diff' => $this->updated_at->diffForHumans()
93 public function getSubCommentsAttribute() {
94 return $this->sub_comments;