Skip to main content

Rendering non-Comments in the list

Sometimes you might want to render non-Comments in the list of comments. For example, you might want to render when the status of a project is changed. For this, you can override the getComments method in your model, and return instances of the Kirschbaum\Commentions\RenderableComment data object.

use Kirschbaum\Commentions\RenderableComment;

public function getComments(?int $limit = null): Collection
{
$statusHistory = $this->statusHistory()->get()->map(fn (StatusHistory $statusHistory) => new RenderableComment(
id: $statusHistory->id,
authorName: $statusHistory->user->name,
body: sprintf('Status changed from %s to %s', $statusHistory->old_status, $statusHistory->new_status),
createdAt: $statusHistory->created_at,
));

$comments = $this->comments()->latest()->with('author')->get();

$mergedCollection = $statusHistory->merge($comments);

if ($limit) {
return $mergedCollection->take($limit);
}

return $mergedCollection;
}