ronnytorresmtz/comments

Laravel Nova 资源工具。

v1.0.8 2022-02-07 22:39 UTC

This package is not auto-updated.

Last update: 2024-10-01 11:30:21 UTC


README

评论

允许用户为资源添加评论。它管理后端的分页

如何使用

    Comments::make()
        //->showOnlyMyComments(true)
        //->showOnlyMyComments(false)
        ->showOnlyMyComments() //default=true
        //->allowToShowAllCompaniesComments()  // Could be use for the system admin
        ->per_page(3),

如何安装

1) 使用 composer 安装组件,运行以下命令

    composer require ronnytorres/comments

2) 在用户表中添加 company_id 字段,它可以是 null

    $table->bigInteger('company_id')->nullable();

3) 组件需要创建一个 Comment 模型。

    <?php

    namespace App\Models;

    use Illuminate\Database\Eloquent\Model;
    use Illuminate\Database\Eloquent\SoftDeletes;

    class Comment extends Model
    {
        use SoftDeletes;

        protected $dates = ['created_at'];
    }

4) 存储评论的 Nova 资源数据库表创建的迁移存在。

     Schema::create('comments', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->string('resourceName');
        $table->string('resourceId');
        $table->text('text');
        $table->text('company_id')->nullable();
        $table->integer('user_id');
        $table->softDeletes();
        $table->timestamps();

        $table->index(['resourceId','resourceName', 'user_id', 'created_at']);
    });

5) 必须创建一个 CommentPolicy。

    <?php

    namespace App\Policies;

    use App\Models\User;
    use NovaComponents\Comments\Models\Comment;
    use Illuminate\Auth\Access\HandlesAuthorization;

    class CommentPolicy
    {
        use HandlesAuthorization;

        /**
        * Determine whether the user can view any models.
        *
        * @param  \App\Models\User  $user
        * @return mixed
        */
        public function viewAny(User $user)
        {
            return true;
        }

        /**
        * Determine whether the user can view the model.
        *
        * @param  \App\Models\User  $user
        * @param  \App\Models\Comment  $comment
        * @return mixed
        */
        public function view(User $user, Comment $comment)
        {
            return true;
        }

        /**
        * Determine whether the user can create models.
        *
        * @param  \App\Models\User  $user
        * @return mixed
        */
        public function create(User $user)
        {
            return true;
        }

        /**
        * Determine whether the user can update the model.
        *
        * @param  \App\Models\User  $user
        * @param  \App\Models\Comment  $comment
        * @return mixed
        */
        public function update(User $user, Comment $comment)
        {
            return true;
        }

        /**
        * Determine whether the user can delete the model.
        *
        * @param  \App\Models\User  $user
        * @param  \App\Models\Comment  $comment
        * @return mixed
        */
        public function delete(User $user, Comment $comment)
        {
            return true;
        }

        /**
        * Determine whether the user can restore the model.
        *
        * @param  \App\Models\User  $user
        * @param  \App\Models\Comment  $comment
        * @return mixed
        */
        public function restore(User $user, Comment $comment)
        {
            return true;
        }

        /**
        * Determine whether the user can permanently delete the model.
        *
        * @param  \App\Models\User  $user
        * @param  \App\Models\Comment  $comment
        * @return mixed
        */
        public function forceDelete(User $user, Comment $comment)
        {
            return true;
        }
    }

附加信息

  • api.php 文件包含后端代码,而不是控制器。

资源示例

Image