0

如何在现有模型中引入 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为空。

4

1 回答 1

0

当你以这样的形式拥有它时

<form …&gt;
  <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,
]);
于 2020-04-11T00:44:36.140 回答