Comment

Introduction

The Assely framework comes with App\Comments repository class that allows for managing WordPress comments.

This singularity, unlike other, is registered by default inside App\Providers\SingularityServiceProvider class.

Columns on the list of Comments

The columns method inside app\Comments.php file allows to define custom columns which are displayed on the list of comments.

Comments accepts one kind of column: metabox.

public function columns()
{
    return [
        Column::metabox('App\Metaboxes\CommentDetails', 'helpful-flag'),
    ];
}

Using Comments Repository

Comment facade provides access to the comments. However, you can also inject the App\Comments repository class.

use App\Comments;

public function index(Comments $comments) {
    $comment = $comments->find(1);
}

Quering Comment

In this section you will learn how you can query comments in your application.

Fetching multiple Comments

The all method will retrive all of the comments. This method returns an instance of Illuminate\Support\Collection, so you can easily modify results with diffrent helper methods.

$comments = Comment::all();

Retrieving single Comment

You can also immediately search for single comment. Pass comment id as the find method argument.

$comment = Comment::find(1);
Not Found Query Exception

Sometimes you want to throw an exception when comment was not found. The findOrFail method returns found comment, but if not Assely\Database\QueryException will be thrown.

Comment::findOrFail(1);

Custom Query

Needs something more complex? You can use query method with array of parameters as argument.

Comment::query([
    'author_email' => 'john.smith@example.com',
]);

Inserting Comments

Use create method with array of properties and values for inserting new comment to the database.

Comment::create([
    'author' => 'john.smith@example.com',
]);
Inserting Query Exception

The createdOrFail method will insert new comment, but when error occurs Assely\Database\QueryException will be thrown.

Comment::createOrFail([
    'author' => 'john.smith@example.com',
]);

Updating Comments

To update a comment, you need to set new property values on retrived Assely\Adapter\Comment instance, and then call save method.

$comment = Comment::find(1);

$comment->author = 'John Smith';

$comment->save();

Deleting Comment

Call destroy method on retrieved Assely\Adapter\Comment instance to remove comment from database.

$comment = Comment::find(1);

$comment->destroy();

Displaying comments in a view

You have access to bunch of diffrent properties. List of all you can find in adapters documentation.

<div class="comment">
    <span>{{ $comment->author }}</span>
    <p>{{ $comment->content }}</p>
</div>

Rendering Comment replies

@foreach($comment->replies() as $reply)
    <span>{{ $reply->author }}</span>
    <p>{{ $reply->content }}</p>
@endforeach

Utilizing view templates from resources/views/comments

Every Assely installation comes with ready to use comments disqusion, replies and form views. This way you have full control over all elements. No more messy comments_form() function.

Pass to the view current post instance.

View::make('post', [
    'post' => Post::find(1)
]);

Inside view include resources/views/comments/disqusion.blade.php template with post id and replies.

@include('comments.disqusion', [
    'id' => $post->id,
    'comments' => $post->comments
]);