PHP code example of kirschbaum-development / commentions
1. Go to this page and download the library: Download kirschbaum-development/commentions library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
kirschbaum-development / commentions example snippets
use Kirschbaum\Commentions\Contracts\Commenter;
class User extends Model implements Commenter
{
// ...
}
use Kirschbaum\Commentions\HasComments;
use Kirschbaum\Commentions\Contracts\Commentable;
class Project extends Model implements Commentable
{
use HasComments;
}
// Make comments readonly
CommentsEntry::make()
->readonly()
->mentionables(User::all())
CommentsAction::make()
->readonly()
->mentionables(User::all())
CommentsTableAction::make()
->readonly()
->mentionables(User::all())
// You can also conditionally enable readonly mode
CommentsAction::make()
->readonly(auth()->user()->cannot('create', Comment::class))
->mentionables(User::all())
use Kirschbaum\Commentions\Filament\Actions\SubscriptionAction;
// In header actions
protected function getHeaderActions(): array
{
return [
SubscriptionAction::make(),
];
}
// In table actions (Filament 3)
->actions([
SubscriptionTableAction::make(),
])
// In record actions (Filament 4)
->recordActions([
SubscriptionAction::make(),
])
// Hide the sidebar entirely
<livewire:commentions::comments :mentionables="App\Models\User::all()" :record="$record" :sidebar-enabled="false" />
// Keep the sidebar, but hide the subscribers list (uses config default if omitted)
<livewire:commentions::comments :mentionables="App\Models\User::all()" :record="$record" :show-subscribers="false" />
// Hide the sidebar entirely
<livewire:commentions.comments :mentionables="App\Models\User::all()" :record="$record" :sidebar-enabled="false" />
// Keep the sidebar, but hide the subscribers list (uses config default if omitted)
<livewire:commentions.comments :mentionables="App\Models\User::all()" :record="$record" :show-subscribers="false" />
use Kirschbaum\Commentions\Filament\Actions\CommentsAction;
->recordActions([
CommentsAction::make()
->mentionables(User::all())
->disableSidebar()
])
// Subscribe a user
$commentable->subscribe($user);
// Unsubscribe a user
$commentable->unsubscribe($user);
// Check if a user is subscribed
$isSubscribed = $commentable->isSubscribed($user);
// Get all subscribers
$subscribers = $commentable->getSubscribers();
use Kirschbaum\Commentions\Filament\Actions\CommentsAction;
->recordActions([
CommentsAction::make()
->mentionables(User::all())
->perPage(10)
])
use Kirschbaum\Commentions\Filament\Actions\CommentsAction;
->recordActions([
CommentsAction::make()
->mentionables(User::all())
->disablePagination();
])
use Filament\Models\Contracts\HasName;
use Kirschbaum\Commentions\Contracts\Commenter;
class User extends Model implements Commenter, HasName
{
public function getFilamentName(): string
{
return (string) '#' . $this->id . ' - ' . $this->name;
}
}
use Kirschbaum\Commentions\Contracts\Commenter;
class User extends Model implements Commenter
{
public function getCommenterName(): string
{
return (string) '#' . $this->id . ' - ' . $this->name;
}
}
use Filament\Models\Contracts\HasAvatar;
class User extends Authenticatable implements Commenter, HasName, HasAvatar
{
public function getFilamentAvatarUrl(): ?string
{
return $this->avatar_url;
}
}
'attachments' => [
'enabled' => env('COMMENTIONS_ATTACHMENTS_ENABLED', false),
// Filesystem disk and directory used to store uploads.
'disk' => env('COMMENTIONS_ATTACHMENTS_DISK', 'public'),
'directory' => env('COMMENTIONS_ATTACHMENTS_DIRECTORY', 'commentions-attachments'),
// Maximum size per file, in kilobytes.
'max_size' => (int) env('COMMENTIONS_ATTACHMENTS_MAX_SIZE', 10240),
// Maximum number of files per comment.
'max_files' => (int) env('COMMENTIONS_ATTACHMENTS_MAX_FILES', 5),
// Accepted MIME types, validated against the file's actual contents.
'accepted_mime_types' => [
'image/jpeg', 'image/png', 'image/gif', 'image/webp', 'application/pdf',
// ...
],
],
use Kirschbaum\Commentions\Filament\Infolists\Components\CommentsEntry;
CommentsEntry::make('comments')->mentionables(User::all())->enableAttachments();
CommentsEntry::make('comments')->mentionables(User::all())->enableAttachments(fn () => auth()->user()->isAdmin());
CommentsEntry::make('comments')->mentionables(User::all())->disableAttachments();
namespace App\Listeners;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use App\Notifications\NewCommentNotification;
use Kirschbaum\Commentions\Events\UserIsSubscribedToCommentableEvent;
class SendSubscribedUserNotification implements ShouldQueue
{
use InteractsWithQueue;
public function handle(UserIsSubscribedToCommentableEvent $event): void
{
$event->user->notify(
new NewCommentNotification($event->comment)
);
}
}
namespace App\Listeners;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use App\Notifications\UserMentionedInCommentNotification;
use Kirschbaum\Commentions\Events\UserWasMentionedEvent;
class SendUserMentionedNotification implements ShouldQueue
{
use InteractsWithQueue;
public function handle(UserWasMentionedEvent $event): void
{
$event->user->notify(
new UserMentionedInCommentNotification($event->comment)
);
}
}
use Kirschbaum\Commentions\Config;
Config::resolveCommentUrlUsing(function (\Kirschbaum\Commentions\Comment $comment) {
// Return a URL to view the record and scroll to the comment
return route('projects.show', $comment->commentable) . '#comment-' . $comment->getId();
});
use Kirschbaum\Commentions\Config;
Config::resolveAuthenticatedUserUsing(
fn () => auth()->guard('my-guard')->user()
)
$comment->getMentioned()->each(function (Commenter $commenter) {
// do something with $commenter...
});