Skip to content

Add polymorphic threaded comments, tuned to full retrieval & summary, to Laravel 7 and above.

License

Notifications You must be signed in to change notification settings

bmason/threadedComments

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ThreadedComments

License

A polymorphic threaded comment Laravel Service provider with visibility control.

An answer to capturing and summarizing arbitrarily nested comments. Rather than nested set or SQL recursion, all comments contain a root reference(e.g. original Post) along with the standard morphMany relation so it is very efficient to load or summarise an entire reply tree. e.g.

Comment::topRepliesFor([Issue::class, Post::class], strtotime('-1 week'), 'count', 5)

answers an array of up to five objects representing Issue/Post with the greatest number of comments. Objects contain the id and model of all Issues & Posts along with a count of all comments on each and the time of the latest comment within one week. Visibility e.g. this user does not have access to Post 13321 may be added. The result of one simple SQL query (and another per model to obtain titles of the Issues & Posts) could be displayed in a hot topics widget. This performs well with ten thousand post/issues and one hundred thousand comments(< one sec), a daunting challenge for alternatives.

alternatives: SQL recursion Nested Set

Table of Contents

Requirements

  • Laravel 7 or later.

Getting Started

  1. Require the package with Composer.

    $ composer require bpmason/threadedComments
  2. Add the package to your application service providers in config/app.php.

    'providers' => [
    
        Illuminate\Foundation\Providers\ArtisanServiceProvider::class,
        Illuminate\Auth\AuthServiceProvider::class,
        ...
        BMason\threadedComments\ServiceProvider::class,
    
    ],
  3. Publish the package's migrations to your application and migrate.

    $ php artisan vendor:publish --provider="bpmason/threadedComments" --tag="migrations"
    $ php artisan migrate

Usage

use BMason\ThreadedComments\Traits\ThreadedComments

in a model to define instance methods for comments and threadedComments - see the trait for more information

BMason\ThreadedComments\Models\Comment::topRepliesFor($root_type='all',
   $since=null, $orderBy='count', $limit=10, $exclude=null)

for a summary - see the Comment model for more information

say the logged in user does not have access to Post 5 nor 1117

BMason\ThreadedComments\Models\Comment::topRepliesFor('all', null, 'count', 10,
    [['App\Models\Post', [5,1117]]])

will provide a summary ignoring the two posts

Change Logs

first release

Contribution Guidelines

Support follows PSR-12 PHP coding standards, and semantic versioning.

Please report any issue you find in the issues page. Pull requests are welcome.

About

Add polymorphic threaded comments, tuned to full retrieval & summary, to Laravel 7 and above.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 100.0%