3

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

Bartłomiej Semańczyk
  • 59,234
  • 49
  • 233
  • 358
  • The Graham's package is used to work with Markdown data in the back-end. If you want a quick solution for your input element, I think you should find some client-side implements in JavaScript. – Hieu Le Jan 28 '16 at 09:32
  • Thanks. Yes, I want to use markdown client side to input in my form textarea. I want similar to http://demos.krajee.com/markdown-demo –  Jan 28 '16 at 09:56

1 Answers1

2

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) }}
noodles_ftw
  • 1,293
  • 1
  • 9
  • 17
  • Thanks. I want similar to demos.krajee.com/markdown-demo. How can I achieve that? –  Jan 28 '16 at 09:57
  • Do you mean the preview? There's some Javascript involved there, I recommend not use the server-side converter if you want it to be like that. Search for a client side markdown parser. – noodles_ftw Jan 28 '16 at 09:59