Laravel Like Comment: Installation and Usage Guide
This article introduces the Laravel‑like‑comment package, outlines its features, and provides step‑by‑step instructions—including Composer installation, service provider registration, asset publishing, database migration, CSS/JS inclusion, user model configuration, and Blade directives for adding likes and comments—to integrate a full‑featured AJAX comment system into a Laravel application.
Laravel‑like‑comment is an AJAX‑based comment and like system for Laravel projects that requires users to be logged in before they can like, dislike, comment, or manage comment visibility, and it supports user avatars.
Features include liking, disliking, commenting, toggling comment visibility, and displaying user avatars.
Installation is performed via Composer:
<code>composer require risul/laravel-like-comment</code>After installing, add the service provider to your config/app.php providers array:
<code>risul\LaravelLikeComment\LikeCommentServiceProvider::class</code>Publish the package assets:
<code>php artisan vendor:publish</code>Run the migrations to create the necessary tables:
<code>php artisan migrate</code>Include the required Semantic‑UI CSS files and the package’s own stylesheet in the <head> of your pages:
<code><link href="//cdnjs.cloudflare.com/ajax/libs/semantic-ui/2.2.2/components/icon.min.css" rel="stylesheet">
<link href="//cdnjs.cloudflare.com/ajax/libs/semantic-ui/2.2.2/components/comment.min.css" rel="stylesheet">
<link href="//cdnjs.cloudflare.com/ajax/libs/semantic-ui/2.2.2/components/form.min.css" rel="stylesheet">
<link href="//cdnjs.cloudflare.com/ajax/libs/semantic-ui/2.2.2/components/button.min.css" rel="stylesheet">
<link href="{{ asset('/vendor/laravelLikeComment/css/style.css') }}" rel="stylesheet"></code>Add jQuery (using a domestic CDN if needed) and the package’s script before the closing
<code><script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.min.js"></script>
<script src="{{ asset('/vendor/laravelLikeComment/js/script.js') }}" type="text/javascript"></script></code>Configure the user model path in config/laravelLikeComment.php (default is 'App\User' ).
In your User model, add a static method to return author information:
<code>/**
* Return the user attributes.
* @return array
*/
public static function getAuthor($id)
{
$user = self::find($id);
return [
'id' => $user->id,
'name' => $user->name,
'email' => $user->email,
'url' => '', // optional
'avatar' => 'gravatar', // default avatar
'admin' => $user->role === 'admin', // bool
];
}</code>To display the like button on a page, insert the Blade include with the appropriate item identifier:
<code>@include('laravelLikeComment::like', ['like_item_id' => 'image_31'])</code>The like_item_id should uniquely identify the content (e.g., post_1 for a post with ID 1).
Similarly, to add a comment box, use:
<code>@include('laravelLikeComment::comment', ['comment_item_id' => 'video_12'])</code>The comment_item_id uniquely identifies the comment target.
After inserting these directives, the package will render the like and comment interfaces, store interactions in the database, and handle user authentication automatically.
php中文网 Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.