🏡 index : ~doyle/koselig.git

author Jordan Doyle <jordan@doyle.wf> 2016-11-27 19:21:17.0 +00:00:00
committer Jordan Doyle <jordan@doyle.wf> 2016-11-27 19:21:17.0 +00:00:00
commit
be112582d242da817d57b236c2ae58daff7c828b [patch]
tree
36aa1401de700aec99271978089e121522a91dd9
parent
a8e3a4e22bcd7c2ed746f2f32c2e4f79d0367eee
download
be112582d242da817d57b236c2ae58daff7c828b.tar.gz

Make comments work a little bit better



Diff

 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 <jordan@doyle.wf>
 */
@@ -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;
    }

    /**