9

Laravel 扩展推荐:Rich Text Laravel 富文本编辑器(Trix)

 2 years ago
source link: https://zhuanlan.zhihu.com/p/414453693
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Laravel 扩展推荐:Rich Text Laravel 富文本编辑器(Trix)

摈弃世俗浮躁,追求技术精湛

转载自 Laravel 论坛:https://learnku.com/laravel/t/61357

Laravel 富文本 集成富文本编辑器,灵感来源于 Rails 的 Action Text 扩展。

这个包提供使用 Trix 编辑器 在关联模型中存储、新建等富文本所需的一切操作:

  • 数据结构使得富文本与模型关联变得简单(使用单独数据库)
  • 支持图片附件
  • 支持 Trix 内容附件
  • 支持内容对象,例如附件,链接,等。
  • 纯文本渲染

这个包相当灵活,允许我们在模型中存储富文本:

  1. 包提供独立的数据库表结构,用以存储模型关联的文本数据;
  2. 使用包的 AsRichTextContent trait 在模型上转换富文本字段。

数据库结构如下:

Schema::create('rich_texts', function (Blueprint $table) {
    $table->id();
    $table->morphs('record');
    $table->string('field');
    $table->longText('body')->nullable();
    $table->timestamps();

    $table->unique(['field', 'record_type', 'record_id']);
});

下面例子是一个 Post 模型,设置两个富文本字段:

use Tonysm\RichTextLaravel\Models\Traits\HasRichText;

class Post extends Model
{
    use HasRichText;

    protected $guarded = [];

    protected $richTextFields = [
        'body',
        'notes',
    ];
}

创建时赋值两个字段,会自动转发存储到 rich_texts 表中:

$post = Post::create(['body' => $body, 'notes' => $notes]);

这个包还负责查询富文本数据:

// 加载所有富文本字段(每个字段有1个查询,因为每个字段都有其关系)
Post::withRichText()->get();

// 仅加载一个字段:
Post::withRichText('body')->get();

// 加载部分字段(非全部):
Post::withRichText(['body', 'notes'])->get();

该软件包的 Readme 提供了大量示例和有关如何开始的说明。您可以了解有关此软件包的更多信息、获取完整的安装说明可以查看 GitHub 上的 源代码

讨论请前往专业的 Laravel 论坛:https://learnku.com/laravel/t/61357


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK