ftechstack/comments

3.4.4 2020-04-27 02:09 UTC

This package is auto-updated.

Last update: 2024-09-27 12:07:25 UTC


README

Comments 是一个 Laravel 包。使用它可以轻松地为您的应用程序实现原生评论。

Become a Patron

概述

此包可用于对应用程序中的任何模型进行评论。

所有评论都存储在一个表中,其中包含一个用于内容的多态关系和一个用于发表评论的用户的多态关系。

功能

  • 查看评论
  • 创建评论
  • 删除评论
  • 编辑评论
  • 回复评论
  • 授权规则
  • 支持本地化
  • 派发事件
  • 路由、控制器、评论、迁移和视图定制
  • 支持非整数 ID
  • 支持多个用户模型
  • 解决 N+1 查询问题
  • 评论审批(可选)
  • 访客评论

截图

以下是一些截图。

无评论 & 访客

无评论 & 登录用户

一条评论

一条评论编辑表单

不同用户的两条评论

教程 & 文章

我计划扩展此章节,添加更多教程和文章。如果您为此包写了些东西,请告诉我,以便我可以更新此章节。

文章

安装

从命令行

composer require laravelista/comments

运行迁移

我们需要创建评论的表。

php artisan migrate

向您的用户模型添加 Commenter 特性

Commenter 特性添加到您的用户模型,以便您可以检索用户的评论

use Laravelista\Comments\Commenter;

class User extends Authenticatable
{
    use Notifiable, Commenter;
}

向模型添加 Commentable 特性

Commentable 特性添加到您想要启用评论的模型

use Laravelista\Comments\Commentable;

class Product extends Model
{
    use Commentable;
}

发布配置 & 配置(可选)

发布配置文件(可选)

php artisan vendor:publish --provider="Laravelista\Comments\ServiceProvider" --tag=config

发布视图(定制)

默认 UI 是为 Bootstrap 4 设计的,但您可以根据需要更改。

php artisan vendor:publish --provider="Laravelista\Comments\ServiceProvider" --tag=views

发布迁移(定制)

您可以发布迁移以允许您对表有更多控制

php artisan vendor:publish --provider="Laravelista\Comments\ServiceProvider" --tag=migrations

用法

在您想要显示评论的视图中,放置此代码并修改它

@comments(['model' => $book])

在上面的示例中,我们将 commentable_type 设置为书籍的类。我们还传递了 commentable_id,即书籍的 id,这样我们就可以知道评论与哪本书相关。幕后,该包会检测当前是否登录了用户。

如果您打开包含上述代码的视图页面,您应该看到一个工作的评论表单。

仅查看已批准的评论

要查看仅批准的评论,请使用此代码

@comments([
    'model' => $book,
    'approved' => true
])

事件

此包会触发事件以让您知道何时发生某些事情。

  • Laravelista\Comments\Events\CommentCreated
  • Laravelista\Comments\Events\CommentUpdated
  • Laravelista\Comments\Events\CommentDeleted

REST API

要更改控制器或路由,请参阅配置。

Route::post('comments', '\Laravelista\Comments\CommentController@store');
Route::delete('comments/{comment}', '\Laravelista\Comments\CommentController@destroy');
Route::put('comments/{comment}', '\Laravelista\Comments\CommentController@update');
Route::post('comments/{comment}', '\Laravelista\Comments\CommentController@reply');

POST /comments

请求数据

'commentable_type' => 'required|string',
'commentable_id' => 'required|string|min:1',
'message' => 'required|string'

PUT /comments/{comment}

  • {comment} - 评论 ID。

请求数据

'message' => 'required|string'

POST /comments/{comment}

  • {comment} - 评论 ID。

请求数据

'message' => 'required|string'

从旧版本更新

支持访客评论

如果您正在更新已存在的数据库表 comments 并希望支持访客评论功能 (新安装默认支持此功能),则创建一个新的迁移命令 php artisan make:migration add_guest_commenting_columns_to_comments_table 并在此粘贴此代码。

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\DB;

class AddGuestCommentingColumnsToCommentsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('comments', function (Blueprint $table) {
            $table->string('commenter_id')->nullable()->change();
            $table->string('commenter_type')->nullable()->change();

            $table->string('guest_name')->nullable();
            $table->string('guest_email')->nullable();
        });
    }
}

最后,运行 php artisan migrate

评论审核支持

如果您正在更新已存在的数据库表 comments 并希望支持评论审核功能 (新安装默认支持此功能),则创建一个新的迁移命令 php artisan make:migration add_approved_column_to_comments_table 并在此粘贴此代码。

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\DB;

class AddApprovedColumnToCommentsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('comments', function (Blueprint $table) {
            $table->boolean('approved')->default(true)->nullable();
        });
    }
}

最后,运行 php artisan migrate

多用户模型支持

如果您正在更新已存在的数据库表 comments 并希望支持多用户模型 (新安装默认支持此功能),则创建一个新的迁移命令 php artisan make:migration add_commenter_type_column_to_comments_table 并在此粘贴此代码。

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\DB;

class AddCommenterTypeColumnToCommentsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('comments', function (Blueprint $table) {
            $table->string('commenter_id')->change();
            $table->string('commenter_type')->nullable();
        });

        DB::table('comments')->update([
            'commenter_type' => '\App\User'
        ]);
    }
}

然后,使用以下命令添加 doctrine/dbal 依赖项:

composer require doctrine/dbal

最后,运行 php artisan migrate

支持非整数 ID

如果您正在更新已存在的数据库表 comments 并希望支持非整型ID (新安装默认支持此功能),则创建一个新的迁移命令 php artisan make:migration allow_commentable_id_to_be_string 并在此粘贴此代码。

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class AllowCommentableIdToBeString extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('comments', function (Blueprint $table) {
            $table->string('commentable_id')->change();
        });
    }
}

然后,使用以下命令添加 doctrine/dbal 依赖项:

composer require doctrine/dbal

最后,运行 php artisan migrate