Summernote所见即所得编辑器无法使用Laravel 5.8正确呈现数据

时间:2019-07-09 15:42:48

标签: laravel laravel-5 summernote laravel-5.8

当我想从数据库渲染数据和图像时遇到问题,我正在使用Summernote和Laravel,我将粘贴控制器和视图的代码,

我的所有选项都正确显示了夏季笔记,但是当我尝试在文本中添加一些内容时,例如将其变为粗体或类似的内容,他将无法正常工作。

控制器:

public function createPost(Request $request){
        $this->validate($request, [

            'title' => 'required',

            'description' => 'required',

        ]);

        $title = $request->input('title');

        $description = $request->input('description');

        $writer = Auth::user()->id;

        $dom = new \DomDocument();

        $dom->loadHtml($description, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);    

        $images = $dom->getElementsByTagName('img');

        foreach($images as $k => $img){

            $data = $img->getAttribute('src');
            list($type, $data) = explode(';', $data);
            list(, $data)      = explode(',', $data);
            $data = base64_decode($data);
            $image_name= "/upload/" . time().$k.'.png';
            $path = public_path() . $image_name;
            file_put_contents($path, $data);
            $img->removeAttribute('src');
            $img->setAttribute('src', $image_name);

        }

        $description = $dom->saveHTML();

        $post = new Post;
        $post->title= $title;
        $post->description = $description;
        $post->writer = $writer;
        $post->save();

        return redirect()->route('home')->with('success', 'Post has been successfully added!'); 
}

添加视图:

        @if(Auth::check())
        <div class="col-md-12">
            <form method="post" action="{{ route('post.form') }}">
                {{ csrf_field() }}
                <div class="form-group">
                    <label for="name">Title</label>
                    <input type="text" class="form-control" id="id_title" name="title"
                           aria-describedby="title" placeholder="Enter title">
                </div>
                <div class="form-group">
                    <label for="description">Description</label>
                    <textarea class="form-control" id="content" rows="3" name="description" placeholder="Description"></textarea>
                </div>
                <button type="submit" class="btn btn-primary">Publish <i class="fas fa-paper-plane"></i></button>
            </form>
        </div>
        @endif

夏季笔记的呼叫:

<script>
    $(document).ready(function() {
        $('#content').summernote({
            height:300,
        });

    });
</script>

结果示例,标题加上内容: example

2 个答案:

答案 0 :(得分:0)

在浏览器中检查输出,也许summernote插件未正确调用,

在head标记中,检查javascript插件以及CSS。正确放置它们。

答案 1 :(得分:0)

您应该使用 {!! $description !!} 打印为 html。