Home » Php » php – How to get Laravel blade view as a string?

php – How to get Laravel blade view as a string?

Posted by: admin April 23, 2020 Leave a comment

Questions:

I need html as string. And I like to use blade engine to store and render html.

This html string needed for conversion it into PDF.

At the moment I have blades streaming as a response back to browsers.

 return view('users.edit', compact('user'));

How can I get my html text from blade template?

Thank you.

How to&Answers:

You can call render() on the view.

$html = view('users.edit', compact('user'))->render();

See the View source code for more information.

Answer:

    <!-- View stored in resources/views/greeting.blade.php -->
    <html>
        <body>
            <h1>Hello, {{ $name }}</h1>
        </body>
    </html>

<!-- In your php controller  -->

    return view('greeting', ['name' => 'James']);