luppakorva/laravel-mentions

更新到用于 Laravel 6 的 jameslkingsley/laravel-mentions。

2.0.1 2021-07-14 20:25 UTC

This package is not auto-updated.

Last update: 2024-10-03 12:08:46 UTC


README

  • 测试于 Laravel 6.11

Build Status

此 Laravel >=5.4 包提供了一种简单的方式来设置 Eloquent 模型的提及。它提供了将提及插入到 content-editable 元素的客户端,将提及与模型关联的后端,以及最后优雅地通知被提及模型它们已被提及的方法。

以下是一些你可以做的简单示例

// Mention a single user
$comment->mention($user);

// Mention a collection of users
$comment->mention($users);

// Handle the form data
$comment->mention($request->mentions);

// Get all mentions, resolved to their models
$comment->mentions();

// Unmention a single user
$comment->unmention($user);

// Unmention a collection of users
$comment->unmention($users);

它还为你处理通知。如果你的提及配置启用了 auto_notify,它将为你处理。如果你需要自己处理逻辑,禁用 auto_notify 并显式地通知提及,例如

$mention = $comment->mention($user);
$mention->notify();

要求

安装

您可以通过以下命令使用 composer 安装此包

composer require jameslkingsley/laravel-mentions

如果您使用的是 Laravel 5.5 或更高版本,此包将被自动发现,但是如果您使用的是低于 5.5 的任何版本,您需要以旧方式注册它

接下来,您必须在 config/app.php 中安装服务提供者

'providers' => [
    ...
    Kingsley\Mentions\MentionServiceProvider::class,
];

现在发布迁移、前端资源和配置

php artisan vendor:publish --provider="Kingsley\Mentions\MentionServiceProvider"

迁移发布后,您可以运行迁移来创建提及表

php artisan migrate

这是发布配置文件的正文

return [
    // The middleware that should be applied to all
    // routes that are registered in this package.
    'middleware' => null,

    // Pools are what you reference on the front-end
    // They contain the model that will be mentioned
    'pools' => [
        'users' => [
            // Model that will be mentioned
            'model' => 'App\User',

            // Filter class that alters the query
            'filter' => null,

            // The column that will be used to search the model
            'column' => 'name',

            // Notification class to use when this model is mentioned
            'notification' => 'App\Notifications\UserMentioned',

            // Automatically notify upon mentions
            'auto_notify' => true
        ]
    ]
];

使用方法

首先,您需要导入 Tribute

npm install tributejs --save-dev

然后,在您的 bootstrap.js 文件中包含 Tribute 并将其全局分配。

import Tribute from "tributejs";
window.Tribute = Tribute;

现在,在您的 bootstrap.js 文件中,您可以导入 Mentions 类并将其全局分配。

import Mentions from "./laravel-mentions";
window.Mentions = Mentions;

现在,将样式导入您的 SCSS 文件中。

@import "laravel-mentions";

现在让我们设置一个表单,我们将在这里写入包含提及的评论

<form method="post" action="{{ route('comments.store') }}">
    <!-- This field is required, it stores the mention data -->
    <input type="hidden" name="mentions" id="mentions" />

    <!-- We write the comment in the div -->
    <!-- The for attribute is a helper to auto-populate the textarea -->
    <textarea class="hide" name="text" id="text"></textarea>
    <div class="has-mentions" contenteditable="true" for="#text"></div>

    <button type="submit">
        Post Comment
    </button>

    <!-- CSRF field for Laravel -->
    {{ csrf_field() }}
</form>

接下来添加初始化提及的脚本

new Mentions({
    // Additional headers to send
    // to possibly authenicate
    // the current user
    http: {
        headers: [
            // {
            //     name: "Authorization",
            //     value: "Bearer your-user-api-token"
            // }
        ]
    },

    // Input element selector
    // Defaults to .has-mentions
    input: ".has-mentions",

    // Output form field selector
    // Defaults to #mentions
    output: "#mentions",

    // Pools
    pools: [
        {
            // Trigger the popup on the @ symbol
            // Defaults to @
            trigger: "@",

            // Pool name from the mentions config
            pool: "users",

            // Same value as the pool's 'column' value
            display: "name",

            // The model's primary key field name
            reference: "id"
        }
    ]
});

现在转向后端。选择您想要分配提及的模型。在这个例子中,我将选择 Comments。我们将导入特质并在类中使用它。

namespace App;

use Illuminate\Database\Eloquent\Model;
use Kingsley\Mentions\Traits\HasMentions;

class Comment extends Model
{
    use HasMentions;
}

接下来切换到存储评论的控制器。在这种情况下,它是 CommentController。创建评论,之后只需调用 mention 方法。

public function store(Request $request)
{
    // Handle the comment creation however you like
    $comment = Comment::create($request->all());

    // Call the mention method with the form mentions data
    $comment->mention($request->mentions);
}

就是这样!现在,当显示您的评论时,您可以通过 Tribute 插入的 .mention-node 类来设置样式。相同的节点还具有一个包含池名称和引用值的 data-object 属性,例如:users:1

使用提及编辑内容

您可能需要编辑文本内容,因此需要在表单中恢复提及列表。这很简单

<input
    type="hidden"
    name="mentions"
    id="mentions"
    value="{{ $comment->mentions()->encoded() }}"
/>

然后,在后台,您可以通过以下方式更新模型的通知

$comment
    ->clearMentions()
    ->mention($request->mentions);

通知

如果您想使用通知,这里有一些您可能需要知道的事情。

  • 当一个提及被通知时,它将使用 Laravel 内置的 Notification 特质来制作通知。这意味着在池的配置中定义的模型类必须具有 Notifiable 特质。
  • 它将使用池配置中定义的通知类,因此您可以为每个类处理它。
  • 通知中存储的数据始终是进行提及的模型,例如 $comment->mention($user) 将在数据字段中存储 $comment
  • 通知类的构造函数接收进行提及的模型作为参数,例如 $comment->mention($user) 将在构造函数中获得 $comment

过滤器

您可能想要在模型检索记录时对模型应用一些自定义过滤器。为此,只需在您的应用程序中的某个位置创建一个类,然后将其添加到提及配置中

return [
    'pools' => [
        'users' => [
            ...
            'filter' => 'App\Filters\UserFilter',
            ...
        ]
    ]
];

这是您过滤器类应有的样子。它只有一个静态方法叫做 handle,该方法接受查询作为参数,并必须返回查询。

<?php

namespace App\Filters;

class UserFilter
{
    /**
     * Handles the filtering and returns the updated query.
     *
     * @return Illuminate\Database\Eloquent\Builder
     */
    public static function handle($query)
    {
        // Apply your filters here!
        return $query->where('someColumn', 'someValue');
    }
}

资源

如果您想更改 /api/mentions 路由返回的 JSON 响应,您可以创建自己的资源类。首先,访问 Laravel 文档 创建并设置资源类。

一旦您有了资源类,只需将其添加到您的提及配置中的一个或多个池中,例如

return [
    'pools' => [
        'users' => [
            ...
            'resource' => 'App\Resources\UserCollection',
            ...
        ]
    ]
];

中间件

如果您愿意,可以可选地给 /api/mentions 路由添加中间件。如果您想在身份验证保护器后面保护路由,这可能会很有用。访问 Laravel 文档 了解更多关于中间件的信息。

return [
    'middleware' => [
        'your-middleware-here',
    ],
    //
];