Blog Detail

09

Jun
Easily Associate Comments with Eloquent Models in Laravel cover image

arrow_back Easily Associate Comments with Eloquent Models in Laravel

Spatie has introduced an amazing package called Laravel Comments. Using this package, you can create comments and associate them with Eloquent models. It comes with batteries included:

  • A beautiful Livewire component to display comments
  • Markdown submission is supported, we’ll render it as HTML
  • Code snippets that appear in comments will automatically be highlighted
  • Users can react to comments (?, ❤️, or any emoji you want)
  • Optionally, you enable a comment approval flow
  • Sane API for creating your own commenting UI
  • Livewire components out of the box

Installation & setup

Getting a license

In order to install Laravel comments, you’ll need to get a license first.

Installation via Composer

First, add the satis.spatie.be repository in your composer.json.

"repositories": [
    {
        "type": "composer",
        "url": "https://satis.spatie.be"
    }
],

Next, you need to create a file called auth.json and place it either next to the composer.json file in your project, or in the composer home directory. You can determine the composer home directory on *nix machines by using this command.

composer config --list --global | grep home

This is the content you should put in auth.json:

{
    "http-basic": {
        "satis.spatie.be": {
            "username": "<YOUR-SPATIE.BE-ACCOUNT-EMAIL-ADDRESS-HERE>",
            "password": "<YOUR-LICENSE-KEY-HERE>"
        }
    }
}

To validate if Composer can read your auth.json you can run this command:

composer config --list --global | grep satis.spatie.be

If you did everything correctly, the above command should display your credentials.

Now, you can install the package via composer:

composer require spatie/laravel-comments

Publishing the config file

Optionally, you can publish the health config file with this command.

php artisan vendor:publish --tag="comments-config"

Migrating the Database

To create the tables used by this package, you must create and run the migration.

php artisan vendor:publish --tag="comments-migrations"
php artisan migrate

Usage Example

Here’s how you can create a comment for the currently logged-in user.

$yourModel->comment('This my comment');

Here’s how to retrieve them all;

$comments = $yourModel->comments
$comment->text // returns "This is my comment"

You can also react to a comment

$comment->react('?');

For more details, you can visit it’s documentation here.

Note

Codebrisk Laravel developers are always ready to execute even your audacious ideas into reality. Our expert team can design and develop custom CRM software, eCommerce, and SASS web applications to meet your needs and transform your customer’s experience. You can Get in Touch with our team to discuss your projects or you can Launch a Project with us.

Published at : 09-06-2022

Author : Rizwan Aslam
AUTHOR
Rizwan Aslam

I am a highly results-driven professional with 12+ years of collective experience in the grounds of web application development especially in laravel, native android application development in java, and desktop application development in the dot net framework. Now managing a team of expert developers at Codebrisk.

Launch your project

Launch project