๐ Hello everyone!
๐ Today, let's delve into the world of Laravel comments using the fantastic package from beyondcode
.
๐ This package allows seamless integration of commenting functionality into your Laravel applications. Users can comment on various entities like posts, articles, or any other model in your application.
Let's get started! ๐
Installation via Composer
composer require beyondcode/laravel-comments
The package will automatically register itself.
You can publish the migration with:
php artisan vendor:publish --provider="BeyondCode\Comments\CommentsServiceProvider" --tag="migrations"
After the migration has been published you can create the media-table by running the migrations:
php artisan migrate
You can publish the config-file with:
php artisan vendor:publish --provider="BeyondCode\Comments\CommentsServiceProvider" --tag="config"
Usage
Registering Models
To let your models be able to receive comments, add the HasComments trait to the model classes. ๐
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use BeyondCode\Comments\Traits\HasComments;
class Post extends Model
{
use HasComments;
...
}
Commenting on entities
To create a comment on your commentable models, you can use the comment
method. It receives the string of the comment that you want to store. ๐
use App\Models\Post;
use BeyondCode\Comments\Traits\HasComments;
class YourModel extends Model
{
use HasComments;
}
$post = Post::find(1);
$post->comment('This is a comment');
$post->commentAsUser($user, 'This is a comment from someone else');
Retrieving Comments
$post = Post::find(1);
// Retrieve all comments
$comments = $post->comments;
// Retrieve only approved comments
$approved = $post->comments()->approved()->get();
Additional Resources
Check out the official README for more details and advanced usage.
๐ Stay tuned for more updates by following me on GitHub! ๐
๐ Don't forget to share your thoughts and experiences in the comments below! ๐ฌ
Top comments (0)