PHP code example of happenv-com / filament-comments

1. Go to this page and download the library: Download happenv-com/filament-comments 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/ */

    

happenv-com / filament-comments example snippets


use Happenv\FilamentComments\Filament\Components\Comments;

$schema->components([
    Comments::make(), // uses the record's `comments` relationship
]);

return [
    /*
     * Model of comment authors. Defaults to the model of the "users" auth provider.
     */
    'author_model' => null,

    /*
     * Model of comments. Must extend Happenv\FilamentComments\Models\Comment,
     * e.g. to add HasUuids together with a matching migration.
     */
    'comment_model' => null,
];

'author_model' => App\Models\Admin::class,

namespace App\Models;

use Happenv\FilamentComments\Models\Comment as BaseComment;
use Illuminate\Database\Eloquent\Concerns\HasUuids;

class Comment extends BaseComment
{
    use HasUuids;
}

// config/filament-comments.php
'comment_model' => App\Models\Comment::class,

use Happenv\FilamentComments\Concerns\HasComments;

class Issue extends Model
{
    use HasComments;
}

use Happenv\FilamentComments\Models\Comment;
use Illuminate\Database\Eloquent\Relations\MorphMany;

public function internalNotes(): MorphMany
{
    return $this->morphMany(Comment::class, 'commentable')
        ->where('content', 'like', '%[internal]%');
}

use Happenv\FilamentComments\Filament\Components\Comments;

public static function infolist(Schema $schema): Schema
{
    return $schema->components([
        Comments::make(), // uses the `comments` relationship
    ]);
}

use Happenv\FilamentComments\Enums\CommentFormat;
use Happenv\FilamentComments\Enums\CommentFormLocation;
use Happenv\FilamentComments\Enums\CommentsPaginationLocation;
use Happenv\FilamentComments\Enums\CommentsPaginationType;
use Happenv\FilamentComments\Filament\MentionProviders\UserMentionProvider;

Comments::make('internalNotes')
    ->formLocation(CommentFormLocation::Below)              // Above (default) or Below
    ->paginationType(CommentsPaginationType::Standard)      // Simple (default), Standard or Cursor
    ->paginationLocation(CommentsPaginationLocation::Both)  // Above, Below (default), Both or None
    ->paginationOptions([10, 25, 50])                       // page sizes the user can pick
    ->paginationDefaultPerPage(25)                          // must be one of the options
    ->commentFormat(CommentFormat::Markdown)                // Html (default) or Markdown
    ->sortColumn('created_at')                              // newest first, ties broken by the primary key
    ->commentItemContentFieldName('content')                // attribute that holds the comment
    ->mentionProviders([UserMentionProvider::class])
    ->canComment(fn (Issue $record): bool => auth()->user()->can('comment', $record));

Comments::make()->formLocation(CommentFormLocation::Below);

Comments::make()
    ->paginationType(CommentsPaginationType::Cursor)
    ->paginationLocation(CommentsPaginationLocation::Above)
    ->paginationOptions([5, 10, 25])
    ->paginationDefaultPerPage(10);

Comments::make()->sortColumn('updated_at');

Comments::make()->commentFormat(CommentFormat::Markdown);

Comments::make()->commentItemContentFieldName('body');

Comments::make()->canComment(fn (Issue $record): bool => auth()->user()->can('comment', $record));

use Happenv\FilamentComments\Filament\MentionProviders\UserMentionProvider;

Comments::make()->mentionProvider(UserMentionProvider::class);

// or several at once; both methods add to the list
Comments::make()->mentionProviders([UserMentionProvider::class, TeamMentionProvider::class]);

use Filament\Forms\Components\RichEditor\MentionProvider;
use Happenv\FilamentComments\Contracts\ProvidesMentions;

class TeamMentionProvider implements ProvidesMentions
{
    public static function make(): MentionProvider
    {
        return MentionProvider::make('#')
            ->getSearchResultsUsing(fn (string $search): array => Team::query()
                ->where('name', 'like', "%{$search}%")
                ->pluck('name', 'id')
                ->all())
            ->getLabelsUsing(fn (array $ids): array => Team::query()
                ->whereIn('id', $ids)
                ->pluck('name', 'id')
                ->all());
    }
}

use Closure;
use Filament\Forms\Components\RichEditor\RichContentAttribute;
use Happenv\FilamentComments\Models\Comment;

class Issue extends Model
{
    use HasComments;

    public function setUpCommentsRichContent(Comment $comment): Closure
    {
        return fn (RichContentAttribute $attribute): RichContentAttribute => $attribute
            ->mentions([TeamMentionProvider::make()]);
    }
}

use Happenv\FilamentComments\Actions\SaveCommentAction;
use Happenv\FilamentComments\Contracts\SavesComment;
use Happenv\FilamentComments\Support\CommentsSettings;

class SaveAndNotify implements SavesComment
{
    public function __invoke(Model $record, array $data, Authenticatable $author, CommentsSettings $settings): Model
    {
        $comment = app(SaveCommentAction::class)($record, $data, $author, $settings);

        $record->owner->notify(new NewComment($comment));

        return $comment;
    }
}

Comments::make()->saveAction(SaveAndNotify::class);

use Filament\Forms\Components\RichEditor;
use Happenv\FilamentComments\Filament\Schemas\CommentFormSchema;

class CompactCommentForm extends CommentFormSchema
{
    public static function htmlFormatInput(string $contentField, array $mentions): RichEditor
    {
        return parent::htmlFormatInput($contentField, $mentions)
            ->toolbarButtons(['bold', 'italic', 'link']);
    }
}

Comments::make()->formSchema(CompactCommentForm::class);

use Filament\Schemas\Schema;
use Happenv\FilamentComments\Contracts\ConfiguresCommentItem;
use Happenv\FilamentComments\Support\CommentsSettings;

class CompactCommentItem implements ConfiguresCommentItem
{
    public static function configure(Schema $schema, CommentsSettings $settings): Schema
    {
        return $schema->components([
            $settings->itemComponent::make($settings->contentField)
                ->footerComponent(false),
        ]);
    }
}

Comments::make()->itemSchema(CompactCommentItem::class);

use Filament\Infolists\Components\TextEntry;
use Filament\Schemas\Components\Component;
use Happenv\FilamentComments\Filament\Components\Comment;

class CommentWithEmail extends Comment
{
    public function getDefaultAuthorComponent(): Component | false | null
    {
        return TextEntry::make('author.email')->hiddenLabel();
    }
}

Comments::make()->commentItemComponent(CommentWithEmail::class);

use Happenv\FilamentComments\Models\Comment;

public function register(): void
{
    $this->app->bind(Comment::class, App\Models\Comment::class);
}

use Happenv\FilamentComments\Models\Comment;

$comment = Comment::factory()
    ->for($issue, 'commentable')
    ->withAuthor($user)
    ->create(['content' => '<p>Looks good</p>']);
bash
php artisan vendor:publish --tag=happenv-filament-comments-migrations
php artisan migrate
css
@source '../../../../vendor/happenv-com/filament-comments/resources/**/*.blade.php';
bash
php artisan vendor:publish --tag=happenv-filament-comments-config
bash
php artisan vendor:publish --tag=happenv-filament-comments-views
php artisan vendor:publish --tag=happenv-filament-comments-translations
bash
php artisan vendor:publish --tag=happenv-filament-comments-translations