PHP code example of businesstilto / commentable

1. Go to this page and download the library: Download businesstilto/commentable 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/ */

    

businesstilto / commentable example snippets




return [
    /*
    |--------------------------------------------------------------------------
    | Comment model
    |--------------------------------------------------------------------------
    */
    'comment' => [
        'model' => Tilto\Commentable\Models\Comment::class,
        'policy' => Tilto\Commentable\Policies\CommentPolicy::class,
    ],

    /*
    |--------------------------------------------------------------------------
    | Reply
    |--------------------------------------------------------------------------
    */
    'reply' => [
        'allow_self_reply' => false,
    ],

    /*
    |--------------------------------------------------------------------------
    | Reaction
    |--------------------------------------------------------------------------
    */
    'reaction' => [
        'model' => Tilto\Commentable\Models\CommentReaction::class,
        'allowed' => ['👍', '❤️', '😂', '😮', '😢', '🤔'],
    ],
];

use Tilto\Commentable\Contracts\Commenter;
use Tilto\Commentable\Traits\IsCommenter;

class User extends Model implements Commenter
{
    use isCommenter;
}

use Tilto\Commentable\Traits\HasComments;
use Tilto\Commentable\Contracts\Commentable;

class Post extends Model implements Commentable
{
    use HasComments;
}

namespace App\Policies;

use Tilto\Commentable\Contracts\Commenter;
use Tilto\Commentable\Models\Comment;
use Tilto\Commentable\Policies\CommentPolicy as CommentablePolicy;

class CommentPolicy extends CommentablePolicy
{
    public function create(Commenter $user): bool
    {
        // ...
    }

    public function update(Commenter $user, Comment $comment): bool
    {
        // ...
    }

    public function reply(Commenter $user, Comment $comment): bool
    {
        // ...
    }

    public function delete(Commenter $user, Comment $comment): bool
    {
        // ...
    }
}

'comment' => [
    'model' => Tilto\Commentable\Models\Comment::class,
    'policy' => App\Policies\CommentPolicy::class,
],

use Tilto\Commentable\Events\CommentCreatedEvent;

class NotifyOnCommentCreated
{
    public function handle(CommentCreatedEvent $event): void
    {
        // Access the created comment:
        $comment = $event->comment;

        // Add your custom logic here, e.g., send a notification
    }
}

protected $listen = [
    CommentCreatedEvent::class => [
        NotifyOnCommentCreated::class,
    ],
];

use Tilto\Commentable\Filament\Infolists\Components\CommentsEntry;

CommentsEntry::make('comments')

CommentsEntry::make('comments')
    ->buttonPosition('right') // Options: 'left', 'right'

CommentsEntry::make('comments')
    ->toolbarButtons([
        ['bold', 'italic', 'strike'],
        ['attachFiles'],
    ])

CommentsEntry::make('comments')
    ->toolbarButtons([])

CommentsEntry::make('comments')
    ->markdownEditor()

// In your Commentable model (e.g., Post):
use Filament\Forms\Components\RichEditor\MentionProvider;

public function getCommentMentionProviders(): array|null
{
    return [
        MentionProvider::make('@')
            ->getSearchResultsUsing(fn(string $search): array => User::query()
                ->where('name', 'like', "%{$search}%")
                ->orderBy('name')
                ->limit(10)
                ->pluck('name', 'id')
                ->all())
            ->getLabelsUsing(fn(array $ids): array => User::query()
                ->whereIn('id', $ids)
                ->pluck('name', 'id')
                ->all()),
        MentionProvider::make('#')
            ->items([
                1 => 'How to Bake Bread',
                2 => 'Laravel Tips & Tricks',
                3 => 'The Future of PHP',
                4 => '10 Best Coding Practices',
                5 => 'Debugging 101',
                6 => 'Deploying with Docker',
            ]),
    ];
}

// In your CommentsEntry component:
CommentsEntry::make('comments')
    ->mentions()

public function getRenderMentionProviders(): array|null
{
    return [
        MentionProvider::make('@')
            ->getLabelsUsing(fn(array $ids): array => User::query()
                ->whereIn('id', $ids)
                ->pluck('name', 'id')
                ->all())
            ->url(fn(string $id, string $label): string => route('filament.portal.resources.users.view', $id))
    ];
}

CommentsEntry::make('comments')
    ->fileAttachmentsDisk('public') // Set the storage disk for attachments
    ->fileAttachmentsVisibility('public') // Set the storage visibility for attachments
    ->fileAttachmentsDirectory('comments') // Set the directory for attachments
    ->fileAttachmentsAcceptedFileTypes(['pdf', 'jpg', 'png']) // Set accepted file types
    ->fileAttachmentsMaxSize(5120) // Set max file size in kilobytes

CommentsEntry::make('comments')
    ->pollInterval('5s') // Refresh every 5 seconds

CommentsEntry::make('comments')
    ->enablePolling() // Refresh using the default interval

'reaction' => [
    'model' => Tilto\Commentable\Models\CommentReaction::class,
    'allowed' => ['👍', '❤️', '😂', '😮', '😢', '🤔'],
],

CommentsEntry::make('comments')
    ->reactions() // Enable reactions (default)
    ->disableReactions() // Disable reactions

CommentsEntry::make('comments')
    ->allowedReactions(['👍', '👎', '🔥'])

CommentsEntry::make('comments')
    ->nestable() // Enable replies

'reply' => [
    'allow_self_reply' => true, // default: false
],
bash
php artisan vendor:publish --tag="commentable-migrations"
php artisan migrate
bash
php artisan vendor:publish --tag="commentable-config"
bash
php artisan vendor:publish --tag="commentable-translations"
bash
php artisan vendor:publish --tag="commentable-views"