How to use Markdown for textarea input field in Laravel 5 form?

梦想的初衷 提交于 2020-04-13 16:49:28

问题


In my Laravel 5 based project, I am using Markdown package from http://packalyst.com/packages/package/graham-campbell/markdown.

How to use Markdown for textarea input field in Laravel 5 form? One good example found for Yii2 but need to know how can achieve in Laravel 5. Markdown demo for Yii2: http://demos.krajee.com/markdown-demo


回答1:


If you want to store the HTML output in the database (you shouldn't IMO), you can do it like this:

<?php

namespace App\Http\Controllers;

use App\SomeModel;
use Illuminate\Http\Request;
use GrahamCampbell\Markdown\Facades\Markdown;

class SomeController extends Controller
{
    /**
     * Handle form submission of my markdown form.
     *
     * @return redirect
     */
    public function create(Request $request)
    {
        $markdownInput = $request->get('markdown_input');

        $model = new SomeModel();
        $model->html = Markdown::convertToHtml($markdownInput);

        if ($model->save()) {
            return redirect('/success');
        }
        else {
            die("Handle failed submission.");
        }
    }
}

But as I said, you shouldn't because it will take a lot of storage IF you have a lot of records in your database. If not, it won't hurt.

Instead, save the raw markdown input in your database without converting it to HTML and convert the input to HTML in your views:

In config/app.php add an alias to the Markdown facade:

'Markdown' => 'GrahamCampbell\Markdown\Facades\Markdown'

Then in your views you can do:

{{ Markdown::convertToHtml($rawMarkdownInputFromTheDatabase) }}


来源:https://stackoverflow.com/questions/35056968/how-to-use-markdown-for-textarea-input-field-in-laravel-5-form

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!