glebred/laravel-vue-simple-comments

Vue Laravel Inertia simple comments
14 1
Install
composer require glebred/laravel-vue-simple-comments
Latest Version:v1.0.1
PHP:^8.1
License:MIT
Last Updated:May 25, 2023
Links: GitHub  ·  Packagist
Maintainer: glebred

Simple comments plugin for Laravel Inertia Vue stack

Latest Version on Packagist Total Downloads

Preview

preview

This package allows you to add comments to your Laravel Inertia Vue stack application. The comment form is implemented using TS and Daisy UI which is a kit of UI components for Tailwind CSS. I made it because I needed a simple comment form for my project. I hope it will be useful for you too.

Requirements

  • inertiajs/vue3
  • Daisy UI,
  • Tailwind
  • Vue-toastification
npm i @inertiajs/inertia-vue3
npm i tailwindcss
npm i vue-toastification
npm i daisyui

Installation

You can install the package via composer:

composer require glebred/laravel-vue-simple-comments

Publish the view

php artisan vendor:publish --provider="GlebRed\LaravelVueSimpleComments\LaravelVueSimpleCommentsServiceProvider"

This will create a

  • Vue view: resources/js/vendor/laravel-vue-simple-comments/Comments.vue
  • Also a migration database/migrations/create_comments_table.php
  • Model app/Models/Comment.php
  • Request app/Http/Requests/CommentRequest.php
  • Resource app/Http/Resources/CommentResource.php

Call the comments section in your Vue component:

Import the component import CommentForm from "@/vendor/laravel-vue-simple-comments/Comments.vue"; and use it in your template, for example:

<CommentForm :commentable="answer" :commentable_type="'answers'"></CommentForm>

Where commentable is the object you want to comment on, and commentable_type is the model name.

Usage

Implement abstact class LaravelVueSimpleComments in your controller. Here is an example:

use GlebRed\LaravelVueSimpleComments\Requests\CommentRequest;
use GlebRed\LaravelVueSimpleComments\LaravelVueSimpleComments;

class CommentsController extends LaravelVueSimpleComments
{

    public function store(CommentRequest $request)
    {
        $validated = $request->validated();

        $this->authorize('create', [Comment::class, $validated['commentable_id'], $validated['commentable_type']]);

        Comment::create([
            'commentable_id' => $validated['commentable_id'],
            'commentable_type' => $validated['commentable_type'],
            'user_id' => $request->user()->id,
            'body' => $validated['body'],
        ]);
        return back()->with('flash', [
            'message' => 'Comment added successfully!',
        ]);
    }

    public function destroy(Comment $comment)
    {
        $this->authorize('delete', $comment);

        $comment->delete();

        return back()->with('flash', [
            'message' => 'Comment deleted successfully!',
        ]);
    }
}

After that add routes to your web.php file:

<?php
use App\Http\Controllers\MyCommentsController;

Route::post('/comments', [MyCommentsController::class, 'store']);
Route::delete('/comments/{comment}', [MyCommentsController::class, 'destroy']);
?>

Don't forget to use this trait in your model:

use GlebRed\LaravelVueSimpleComments\Traits\HasComments;

class News extends Model
{
    use HasComments;
}

And then add comments to your content resource, for example:

class NewsResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
     */
    public function toArray($request)
    {
        return [
            //...
            //order by latest first
            'comments' => CommentResource::collection($this->comments()->orderBy('created_at', 'desc')->get()),
        ];
    }
}

Testing

npm test

Wishlist:

  • Likes
  • Replies

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email gleb.redko@gmail.com instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.

Related Packages

glebred/search-multiselect-input

Searchable multiple select input for Livewire Laravel 8

1,113 10
gabrieloliverio/laravel5-generators

Database metadata-based generators for Laravel 5

1
erirk/paypalpayment

laravel-paypalpayment is simple package help you process direct credit card paym...

0
doctrine/dbal

Powerful PHP database abstraction layer (DBAL) with many features for database s...

631,580,251 9,707
laravel/framework

The Laravel Framework.

580,311,802 34,925