Skip to content

Latest commit

 

History

History
173 lines (120 loc) · 4.78 KB

comment.md

File metadata and controls

173 lines (120 loc) · 4.78 KB

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.

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'),
    ];
}

[alert type="info"]More accurate description about Column facade you can find in column documentation.[/alert]

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);
}

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' => '[email protected]',
]);

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

Comment::create([
    'author' => '[email protected]',
]);
Inserting Query Exception

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

Comment::createOrFail([
    'author' => '[email protected]',
]);

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();

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

$comment = Comment::find(1);

$comment->destroy();

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
]);