PHP code example of x-laravel / commentable

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

    

x-laravel / commentable example snippets


use Illuminate\Database\Eloquent\Model;
use XLaravel\Commentable\Commentable;

class Post extends Model
{
    use Commentable;
}

use Illuminate\Database\Eloquent\Model;
use XLaravel\Commentable\Commenter;

class User extends Model
{
    use Commenter;
}

// Anonymous
$post->addComment('Great post!');

// With a commenter
$post->addComment('Great post!', $user);

// Via Commenter
$user->comment($post, 'Great post!');

// Via Commentable
$post->addReply($comment, 'I agree!', $user);

// Via Commenter
$user->reply($post, $comment, 'I agree!');

$post->rootComments()->paginate(20);

$post->rootComments()->withCount('replies')->with('commenter')->paginate(20);

$comment->replies()->paginate(10);

$comment->replies()->withCount('replies')->with('commenter')->paginate(10);

// All comments on a model (root + replies)
$post->comments()->get();

// All comments posted by a user
$user->comments()->get();

// The parent of a reply
$comment->parent;

// The model a comment belongs to
$comment->commentable;

// Check if a comment is a reply
$comment->isReply(); // true / false

$comment->delete();       // soft delete
$comment->restore();      // restore
$comment->forceDelete();  // permanent delete (cascades to replies via FK)
bash
php artisan migrate