【问题标题】:Fill Trix Editor with value from database用数据库中的值填充 Trix 编辑器
【发布时间】:2021-05-04 10:00:57
【问题描述】:

现在,我正在编写一个编辑博客文章页面

我正在关注此文档: https://github.com/amaelftah/laravel-trix#render-trix-for-existing-model

我从数据库中填充了 $blogposting,该数据库存储了有关此 blogposting 的所有属性。例如:

$blogposting->id
$blogposting->author_id
$blogposting->title
$blogposting->content 

我想让我的 trix-editor 填满$blogposting->content

我需要做什么才能实现这一目标?

@trix($blogposting, 'content') 不起作用!

谢谢!

编辑:dd($blogposting) 的输出显示如下:

App\Blogposting {#1299 ▼
  #connection: "mysql"
  #table: "blogpostings"
  #primaryKey: "id"
  #keyType: "int"
  +incrementing: true
  #with: []
  #withCount: []
  #perPage: 15
  +exists: true
  +wasRecentlyCreated: false
  #attributes: array:10 [▼
    "id" => 27
    "blogcategory_id" => 1
    "url" => "2021-02-test-test-test-8"
    "user_id" => 1
    "title" => "Title Tile"
    "opener" => "Content Opener"
    "opener_picture" => "frq6f0QAPheya0Rue3uD4e9OXswytYj7fhXf6iWh.jpeg"
    "content" => "<div>test test test test</div>"
    "created_at" => "2021-05-02 20:55:30"
    "updated_at" => "2021-05-02 20:55:30"
  ]
  #original: array:10 [▶]
  #changes: []
  #casts: []
  #classCastCache: []
  #dates: []
  #dateFormat: null
  #appends: []
  #dispatchesEvents: []
  #observables: []
  #relations: []
  #touches: []
  +timestamps: true
  #hidden: []
  #visible: []
  #fillable: []
  #guarded: array:1 [▶]
  #savedTrixFields: []
  #savedAttachments: []
}

如您所见,内容在那里:"content" =&gt; "&lt;div&gt;test test test test&lt;/div&gt;"

【问题讨论】:

  • 您是否按照安装指南将@trixassets 放在head 标签中?
  • 是的,我在 head 标签中包含了@trixassets。我是否正确地看到 $blogposting 应该是一个集合而 'content' 是 textarea 字段的名称?我该如何调试呢?我不确定我是否传递了正确的变量。
  • {!! $blogposting->trix('content') !!}
  • 试试上面的代码而不是@trix($blogposting, 'content')
  • 艾哈迈德,谢谢,这就是解决方案! :)

标签: laravel


【解决方案1】:

According to documentation 有多种方法可以为现有模型渲染 trix

<!-- inside view blade file -->
@trix(\App\Post::class, 'content') // where Post is your model

@trix($blogposting, 'content')

{!! $blogposting->trix('content') !!} //must use HasTrixRichText trait in order for $model->trix() method work

{!! app('laravel-trix')->make($blogposting, 'content') !!}

【讨论】:

    猜你喜欢
    • 2022-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-11
    相关资源
    最近更新 更多