This is a drop-in comment system built with Livewire, that works for any model.
You can create comments, reply to them, and edit and delete the comments you made. It's using Alpine.js to minimise network requests, by building an Alpine.js directive to display when a comment was posted, handling deleted users and loading more comments gradually.
The Livewire comments component can then be dropped wherever it's needed and the comments will instantly be enabled.
-
Clone this repository:
git clone git@github.com:Aczeko/livewire-comment-system.git
-
Navigate into the directory:
cd yourproject -
Install dependencies:
npm install composer install
-
Duplicate the .env.example file and save it as .env
- here you can set up your database connection
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=livewire_comment_system DB_USERNAME=root DB_PASSWORD= -
Generate a new Application Key:
php artisan key:generate
-
Migrate the tables and seed the
articlesTable.php artisan migrate --seed
-
Run your build process with:
npm run dev
Since you've migrated and seeded the database with an article, you can take a look at it with /articles/an-article.
There you can see the comment section, and unless you are logged-in, you won't be able to post a comment.
Once you're logged in, you can create, edit or delete a comment and also reply to your own or other peoples comments.
You can easily add a comments section to any part of your application by dropping the Livewire comments component into your Blade file. The comments section will automatically work with any model you specify.
To implement this, follow these steps:
Add the following code snippet to your Blade file where you want the comments section to appear:
<livewire:comments :model="$article" />- Replace
"$article"with the model instance you want to associate with the comments. This could be any Eloquent model, such asArticle,Post,Product, etc. - The model passed will be used to associate the comments with that specific instance.
Ensure your model is properly configured to work with the comments component. Here's an example using an `Article model:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use App\Models\Comment;
class Article extends Model
{
use HasFactory;
public function comments()
{
return $this->morphMany(Comment::class, 'commentable');
}
}
- In this example, the
Articlemodel is set up to have a polymorphic relationship with comments using themorphManymethod. - The comments method defines the relationship, allowing the
Articlemodel to be associated with multiple comments.
