optimistdigital/nova-notes-field

此 Laravel Nova 扩展包为 Nova 的字段集合添加了笔记字段。

3.1.0 2023-08-18 14:07 UTC

README

Latest Version on Packagist Total Downloads

Laravel Nova 扩展包为 Nova 的字段集合添加了笔记字段。

要求

  • php: >=8.0
  • laravel/nova: ^4.0

特性

  • 详情视图上的笔记字段
  • 区分用户添加和系统添加的笔记
  • 通过 UI 或程序化添加笔记的能力
  • 编辑用户创建的笔记的能力
  • 删除用户创建的笔记的能力(带有确认对话框)
  • 自定义占位符支持
  • 设置隐藏或显示“添加笔记”按钮的能力

截图

Detail view

安装

# Install the package via Composer
composer require outl1ne/nova-notes-field

# Run automatically loaded migration(s)
php artisan migrate

用法

HasNotes 特性添加到具有笔记的模型中

use Outl1ne\NovaNotesField\Traits\HasNotes;

class ExampleModel extends Model
{
    use HasNotes;
}

NotesField 添加到相应的资源中

use Outl1ne\NovaNotesField\NotesField;

class SomeResource extends Resource
{
  // ...

  public function fields(Request $request)
  {
    return [
      // ...
      NotesField::make('Notes')
        ->placeholder('Add note') // Optional
        ->addingNotesEnabled(false) // Optional
        ->fullWidth(), // Optional
    ]
  }
}

程序化添加笔记

要程序化添加笔记,请使用 HasNotes 特性提供的方法

/**
 * Creates a new note and attaches it to the model.
 *
 * @param string $note The note text which can contain raw HTML.
 * @param bool $user Enables or disables the use of `Auth::user()` to set as the creator.
 * @param bool $system Defines whether the note is system created and can be deleted or not.
 * @return \Outl1ne\NovaNotesField\Models\Note
 **/
public function addNote($note, $user = true, $system = true)

程序化编辑笔记

要程序化编辑笔记,请使用 HasNotes 特性提供的 editNote 方法

/**
 * Edit a note with the given ID and text.
 *
 * @param int|string $noteId The ID of the note to edit.
 * @param string $text The note text which can contain raw HTML.
 * @return \Outl1ne\NovaNotesField\Models\Note
 **/
public function editNote($noteId, $text)

或者,您也可以通过标准 Eloquent 方法简单地更新内存中已经存在的笔记记录

$note = $notable->notes()->where('id', '=', $noteId)->first();

$note->update([
    'text' => $noteText,
]);

// Or...
$note->text = $noteText;
$note->save();

配置

发布配置

您可以通过运行以下命令来发布配置:

php artisan vendor:publish --provider="Outl1ne\NovaNotesField\NotesFieldServiceProvider" --tag="config"

可用的配置选项(s)

自定义编辑和删除授权

默认情况下,只有写下笔记的用户可以编辑/删除它,并且没有人可以编辑/删除系统笔记。

您可以通过定义一个新的 Laravel 授权门 edit-nova-notedelete-note-note 来定义哪些用户可以编辑/删除哪些笔记。

在您的 AuthServiceProvider.php 中添加一个门定义如下:

use Illuminate\Support\Facades\Gate;
use Outl1ne\NovaNotesField\Models\Note;

// ...

public function boot()
{
  Gate::define('edit-nova-note', function ($user, Note $note) {
    // Do whatever here to add custom edit authorization logic, ie:
    return $note->created_by === $user->id || $user->isAdmin;
  });
  Gate::define('delete-nova-note', function ($user, Note $note) {
    // Do whatever here to add custom delete authorization logic, ie:
    return $note->created_by === $user->id || $user->isAdmin;
  });
}

本地化

可以通过以下发布命令发布翻译文件:

php artisan vendor:publish --provider="Outl1ne\NovaNotesField\NotesFieldServiceProvider" --tag="translations"

您可以将翻译添加到 resources/lang/vendor/nova-notes-field/,通过创建一个带有区域名称的新翻译文件(例如 se.json)并从现有的 en.json 中复制 JSON。

发布迁移(可选)

如果您想编辑迁移(s),您可以像这样发布迁移:

php artisan vendor:publish --provider="Outl1ne\NovaNotesField\NotesFieldServiceProvider" --tag="migrations"

致谢

许可

该项目是开源软件,受MIT 许可协议许可。