Laravel 8 View Render Example

Laravel 8 View Render Example

Hello friends, Today we will show Laravel 8 View Render Example Tutorial. In this post we will learn how to view render in laravel 8. We will provide simple example view render in laravel 8.

Sometimes, we use get html view layout from ajax request. Then you have to first render view file and then you required to store view in variable and then we can return that variable. In following example i  will render view with pass data you can see how i did:

Read also :Laravel 8 Signature Pad Example 

Example : Laravel 8 View Render using Ajax

Step 1 : Create Route

use App\Http\Controllers\ViewRenderController;

Route::get('view', [ViewRenderController::class, 'view']);
Route::post('view-render', [ViewRenderController::class, 'viewRender'])->name('view.render');

Step 2 : Create Controller for Laravel View Render

In this step, you can create to ViewRenderController in controller file.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class ViewRenderController extends Controller
{
    public function view()
    {
    	return view('view');
    }

    public function viewRender(Request $request)
    {
    	$viewRender = view('viewRend')->render();
    return response()->json(array('success' => true, 'html'=>$viewRender));
    }
}

Step 3: Create Render File

resources/views/viewRend.blade.php

<!DOCTYPE html>
<html>
<head>
    <title>Laravel 8 View Render Example - web-tuts.com</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
</head>
<body>
    <div class="container">
        <div class="row">
            <div class="col-md-12">
                <h3>Laravel 8 View Render Example - web-tuts.com</h3>
            </div>
        </div>
    </div>
</body>
</html>

resources/views/view.blade.php

<!DOCTYPE html>
<html>
<head>
    <title>Laravel 8 View Render Example - web-tuts.com</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
</head>
<style type="text/css">
    body{
        background:#fd56c2 !important;
    }
    .box{
        margin-top:200px;
        background:#fff;
        padding:100px 50px;
        text-align: center;
    }
</style>
<body>
    <div class="container">
        <div class="row">
            <div class="col-md-8 offset-md-2 box">
                <div class="viewRender">
        
                </div>
            </div>
        </div>
    </div>
</body>
<script src="https://code.jquery.com/jquery-1.9.1.min.js"></script>
<script type="text/javascript">
    $(document).ready(function(){
         $_token = "{{ csrf_token() }}";
         $.ajax({
         	headers: { 'X-CSRF-Token' : $('meta[name=_token]').attr('content') },
            url: "{{ route('view.render') }}",
            type: 'POST',
            cache: false,
            data: {'_token': $_token },
            datatype: 'html',
            beforeSend: function() {
                //something before send
            },
            success: function(data) {
                console.log(data);
                $('.viewRender').html(data.html);
            }
        });
    });
</script>
</html>

I hope you understand of laravel 8 view render and it can help you…

Read Also :  Laravel Dynamic Google Pie Chart Example