PHP code example of lenorix / laravel-comments

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

    

lenorix / laravel-comments example snippets


use Illuminate\Database\Eloquent\Model;
use Lenorix\LaravelComments\Models\Concerns\HasComments;
use Lenorix\LaravelComments\Models\Concerns\Interfaces\Commentable;

class Post extends Model implements Commentable
{
    use HasComments;

    public function commentableName(): string
    {
        return $this->title;
    }

    public function commentUrl(): string
    {
        return route('posts.show', $this);
    }
}

use Illuminate\Foundation\Auth\User as Authenticatable;
use Lenorix\LaravelComments\Models\Concerns\InteractsWithComments;
use Lenorix\LaravelComments\Models\Concerns\Interfaces\CanComment;

class User extends Authenticatable implements CanComment
{
    use InteractsWithComments;
}

// config/comments.php
'models' => [
    'commentator' => App\Models\User::class,
],

// config/comments.php
'allow_anonymous_comments' => true,

$post->comment('Great read!');                 // as the current authenticated user
$post->comment('Great read!', $anotherUser);    // on behalf of a specific user

$post->comments;                                // all comments, replies 

$comment = $post->comment('Great read!');
$reply = $comment->comment('I agree!');

$comment->comments; // replies to this specific comment

$comment->react('👍');
$comment->react('👍', $anotherUser);

$comment->deleteReaction('👍');

$comment->reactions->summary();
// [['reaction' => '👍', 'count' => 3, 'commentator_reacted' => true], ...]

// config/comments.php
'allowed_attributes' => [
    'p' => ['data-test'],
],

// config/comments.php
'automatically_approve_all_comments' => false,

$comment->isApproved();
$comment->isPending();
$comment->approve();
$comment->reject(); // deletes the comment

Comment::approved()->get();
Comment::pending()->get();

// in a service provider
PendingCommentNotification::sendTo(fn (Comment $comment) => User::where('is_admin', true)->get());

// in a routes file
Route::comments();

use Lenorix\LaravelComments\Enums\NotificationSubscriptionType;

$user->subscribeToCommentNotifications($post, NotificationSubscriptionType::All);
$user->subscribeToCommentNotifications($post, NotificationSubscriptionType::Participating);

$user->unsubscribeFromCommentNotifications($post);
$user->unsubscribeFromAllCommentNotifications();

// config/comments.php
'notifications' => [
    'mail' => [
        'from' => [
            'address' => env('MAIL_FROM_ADDRESS', '[email protected]'),
            'name' => env('MAIL_FROM_NAME', 'Example'),
        ],
    ],
],

// config/comments.php
'mentions' => [
    'enabled' => true,
],

$comment->mentionedCommentators(); // commentator models mentioned in this comment

// config/comments.php
'policies' => [
    'comment' => App\Policies\CustomCommentPolicy::class,
],

use Lenorix\LaravelComments\Events\CommentCreated;
use Lenorix\LaravelComments\Events\CommentDeleted;

Event::listen(function (CommentCreated $event) {
    // $event->comment
});
bash
php artisan vendor:publish --tag="comments-migrations"
php artisan migrate
bash
php artisan vendor:publish --tag="comments-config"
bash
php artisan vendor:publish --tag="comments-views"