softtechnoes/softcomments

Laravel 的评论

资助包维护!
Patreon

安装: 24

依赖者: 0

建议者: 0

安全性: 0

星标: 0

关注者: 1

分支: 0

开放问题: 0

语言:Blade

dev-master 2021-02-03 10:11 UTC

This package is auto-updated.

Last update: 2024-09-29 05:56:16 UTC


README

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

Become a Patron

概览

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

所有评论都存储在一个单独的表中,内容具有多态关系,评论者具有多态关系。

特性

  • 查看评论
  • 创建评论
  • 删除评论
  • 编辑评论
  • 回复评论
  • 授权规则
  • 支持本地化
  • 触发事件
  • 路由、控制器、评论、迁移与视图定制
  • 支持非整数字段
  • 支持多用户模型
  • 解决 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

支持非整数字段

如果您正在更新现有的数据库表 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

Laravelista 赞助商与支持者

我要向以下赞助商和支持者表示感谢,他们为我的开源之旅提供了资金。如果您有兴趣成为赞助商或支持者,请访问 Laravelista 的支持者页面

贡献

感谢您考虑为 Comments 贡献!贡献指南可以在 Laravelista 网站上找到

行为准则

为了确保 Laravelista 社区对所有成员都友好,请阅读并遵守 行为准则

许可证

Comments 是开源软件,受 MIT 许可证 的许可。