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 +++ b/src/Models/Comment.php @@ -2,6 +2,7 @@ namespace Koselig\Models; use Carbon\Carbon; +use Illuminate\Database\Eloquent\Collection; use Illuminate\Database\Eloquent\Model; use Koselig\Support\Action; use Koselig\Support\Wordpress; @@ -29,6 +30,7 @@ use Watson\Rememberable\Rememberable; * @property Post $post post this comment belongs to * @property User $user user this comment belongs to * @property Comment $parent comment this comment is in reply to + * @property Comment[]|Collection $children comments that belong to this comment * * @author Jordan Doyle */ @@ -73,6 +75,16 @@ class Comment extends Model } /** + * 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 @@ class Comment extends Model 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 +++ b/src/Models/Post.php @@ -85,11 +85,18 @@ class Post extends Model /** * 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; } /** -- libgit2 1.7.2