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;
}
use Kirschbaum\Commentions\Filament\Actions\CommentsTableAction;
->actions([
CommentsTableAction::make()
->mentionables(User::all())
])
use Kirschbaum\Commentions\Filament\Actions\CommentsAction;
->recordActions([
CommentsAction::make()
->mentionables(User::all())
])
use Kirschbaum\Commentions\Filament\Actions\CommentsAction;
protected function getHeaderActions(): array
{
return [
CommentsAction::make(),
];
}
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 :record="$record" :sidebar-enabled="false" />
// Keep the sidebar, but hide the subscribers list (uses config default if omitted)
<livewire:commentions::comments :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;
}
}
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...
});