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);
37 public function getPageComments($pageId) {
38 $query = static::newQuery();
39 $query->join('users AS u', 'comments.created_by', '=', 'u.id');
40 $query->leftJoin('users AS u1', 'comments.updated_by', '=', 'u1.id');
41 $query->leftJoin('images AS i', 'i.id', '=', 'u.image_id');
42 $query->selectRaw('comments.id, text, html, comments.created_by, comments.updated_by, '
43 . 'comments.created_at, comments.updated_at, comments.parent_id, '
44 . 'u.name AS created_by_name, u1.name AS updated_by_name, '
45 . 'i.url AS avatar ');
46 $query->whereRaw('page_id = ?', [$pageId]);
47 $query->orderBy('created_at');
51 public function getAllPageComments($pageId) {
52 return self::where('page_id', '=', $pageId)->with(['createdBy' => function($query) {
53 $query->select('id', 'name', 'image_id');
54 }, 'updatedBy' => function($query) {
55 $query->select('id', 'name');
56 }, 'createdBy.avatar' => function ($query) {
57 $query->select('id', 'path', 'url');
61 public function getCommentById($commentId) {
62 return self::where('id', '=', $commentId)->with(['createdBy' => function($query) {
63 $query->select('id', 'name', 'image_id');
64 }, 'updatedBy' => function($query) {
65 $query->select('id', 'name');
66 }, 'createdBy.avatar' => function ($query) {
67 $query->select('id', 'path', 'url');
71 public function getCreatedAttribute() {
73 'day_time_str' => $this->created_at->toDayDateTimeString(),
74 'diff' => $this->created_at->diffForHumans()
79 public function getUpdatedAttribute() {
80 if (empty($this->updated_at)) {
84 'day_time_str' => $this->updated_at->toDayDateTimeString(),
85 'diff' => $this->updated_at->diffForHumans()
90 public function getSubCommentsAttribute() {
91 return $this->sub_comments;