From be112582d242da817d57b236c2ae58daff7c828b Mon Sep 17 00:00:00 2001 From: Jordan Doyle Date: Sun, 27 Nov 2016 19:21:17 +0000 Subject: [PATCH] Make comments work a little bit better --- src/Models/Comment.php | 17 +++++++++++++++++ src/Models/Post.php | 11 +++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/src/Models/Comment.php b/src/Models/Comment.php index 0a4b93a..42606d1 100644 --- a/src/Models/Comment.php +++ a/src/Models/Comment.php @@ -1,7 +1,8 @@ */ @@ -73,6 +75,16 @@ } /** + * Get all the comments that belong to this comment. + * + * @return \Illuminate\Database\Eloquent\Relations\HasMany + */ + public function children() + { + return $this->hasMany(Comment::class, 'comment_parent'); + } + + /** * Get the post that this comment belongs to. * * @return \Illuminate\Database\Eloquent\Relations\BelongsTo @@ -102,6 +114,11 @@ return $this->belongsTo(User::class); } + /** + * Get the comment this comment belongs to. + * + * @return \Illuminate\Database\Eloquent\Relations\BelongsTo + */ public function parent() { return $this->belongsTo(self::class, 'comment_parent'); diff --git a/src/Models/Post.php b/src/Models/Post.php index 2ffd0b9..230fb30 100644 --- a/src/Models/Post.php +++ a/src/Models/Post.php @@ -85,11 +85,18 @@ /** * Get all the comments that belong to this post. * + * @param bool $topLevel only get the top level comments for this post * @return HasMany */ - public function comments() + public function comments(bool $topLevel = true) { - return $this->hasMany(Comment::class, 'comment_post_ID'); + $relation = $this->hasMany(Comment::class, 'comment_post_ID'); + + if ($topLevel) { + $relation->where('comment_parent', 0); + } + + return $relation; } /** -- rgit 0.1.5