PHP code example of alfonsobries / laravel-commentable

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

    

alfonsobries / laravel-commentable example snippets




namespace App\Models;

use Alfonsobries\LaravelCommentable\Contracts\CommentableInterface;
use Alfonsobries\LaravelCommentable\Traits\Commentable;
use Illuminate\Database\Eloquent\Model;


class BlogPost extends Model implements CommentableInterface
{
    use Commentable;
    // ...
}



namespace App\Models;

use Alfonsobries\LaravelCommentable\Contracts\CanCommentInterface;
use Alfonsobries\LaravelCommentable\Traits\CanComment;
use Illuminate\Database\Eloquent\Model;


class User extends Model implements CanCommentInterface
{
    use CanComment;
    // ...
}

$blogPost = BlogPost::first();

$comment = $blogPost->addComment('my comment');

$user = User::first();
$blogPost = BlogPost::first();

$comment = $blogPost->addCommentFrom($user, 'my comment');

$user = User::first();
$blogPost = BlogPost::first();

$comment = $user->comment($blogPost, 'my comment');

$user = User::create([...]);
$user2 = User::create([...]);
$blogPost = BlogPost::first();

$comment = $blogPost->commentFrom($user, 'my comment');
$comment->replyFrom($user2, 'a reply');

$user = User::first();
$blogPost = BlogPost::first();

$comment = $blogPost->addCommentFrom($user, 'my comment');

$user = User::first();
$blogPost = BlogPost::first();

$comment = $user->comment($blogPost, 'my comment');

$comments = User::first()->comments();

$comments = BlogPost::first()->comments();

$popularComments = BlogPost::first()->comments()->popular()->get();

$unpopularComments = BlogPost::first()->comments()->unpopular()->get();

$comment->update(['comment' => 'updated comment']);

$comment->approve();
$comment->unapprove();

$model->comments()->approved()->get();
$model->comments()->notApproved()->get();
console
php artisan vendor:publish --provider="Alfonsobries\LaravelCommentable\LaravelCommentableServiceProvider" --tag="migrations"
console
php artisan vendor:publish --provider="Alfonsobries\LaravelCommentable\LaravelCommentableServiceProvider" --tag="config"
bash
composer analyse