php - Laravel - 在模型中引入 trix 字段
问题描述
如何在现有模型中引入 trix 字段?比如模型有标题、副标题、段落,我想用trix来填充。到目前为止,我对 trix 文本有不同的模型。
class Text_box extends Model{
protected $table = 'TextBox';
protected $fillable = [
'id', 'pages_id','title','subtitle','text'
];}
我希望文本成为一个矩阵字段。
<form method="POST" enctype="multipart/form-data" action="/textbox/{{$page->id}}">
@csrf
<div class="form-group row">
<label for="titlu" class="col-md-4 col-form-label text-md-right">Titlu</label>
<div class="col-md-6">
<input id="titlu" type="text" min="0" class="form-control" name="titlu" required autocomplete="titlu" placeholder="*optional*">
</div>
</div>
<div class="form-group row">
<label for="autor" class="col-md-4 col-form-label text-md-right">Autor</label>
<div class="col-md-6">
<input id="autor" type="text" min="0" class="form-control" name="autor" required autocomplete="autor" placeholder="*optional*">
</div>
</div>
<div style = "height: 50px;"></div>
<div class="row">
<div class="col-lg">
<input id="tb" type="hidden" name="textbox">
<trix-editor input="tb"></trix-editor>
</div>
</div>
<div style = "height: 30px;"></div>
<div class="form-group row">
<div class="col-sm">
<button type="submit" class="btn btn-primary col-sm-2 float-right">
{{ __('Submit') }}
</button>
</div>
</div>
这是我的表格。
public function add_textbox(Request $request, $id)
{
$trix = request()->input('textbox');
return $trix;
$textbox = Text_box::create([
'title' => $request['titlu'],
'subtitle' => $request['autor'],
'text' => $trix['text'],
'pages_id' => $id
]);
$page = Pages::where('id',$id)->first();
$w = json_decode($page->widgets,true);
if($w)
array_push($w,['textbox',$textbox->id]);
else
$w[] = ['textbox',$textbox->id];
$page->widgets = json_encode($w);
$page->save();
return redirect('/edit/'.$id);
}
这是我的控制器。
变量$trix
为空。
解决方案
当你以这样的形式拥有它时
<form …>
<input id="x" type="hidden" name="content">
<trix-editor input="x"></trix-editor>
</form>
你可以像这样在控制器中获取值
$text = request()->input('content');
// and then save it into db
Text_box::create([
//your other fields
'text' => $text,
]);
推荐阅读
- c++ - modeltest + 简单表模式 = 父测试失败
- events - 生命周期事件 prePersist 是否与 Persist 以原子方式播放
- jquery - 如何防止 focusout 事件中的 mousedown 事件?
- swagger - 如何在 swagger 定义中提供字符串数组(默认值)
- python - 使用 SSL/TLS 将 Celery (Django) 连接到 RabbitMQ
- jquery - 如何在开发工具中隐藏这个内联 CSS
- r - 从 jsonlite 扩展通用方法 toJSON
- javascript - 防止用户离开输入字段
- java - 使用 Spring MVC Web 应用程序以 html 形式强制 UTF8 编码
- java - gson - 如何比较忽略几个节点的 JsonObjects?