Laravel 9 View Render Example

10-Apr-2023

.

Admin

Laravel 9 View Render Example

Hi Guys,

In this example, I will learn you how to view render in laravel 9. you can easy and simply view render in laravel 9.

Sometimes, we use get HTML view layout from ajax request. At that, you have to first render the view file and then you need to store the view in a variable, and then we can return that variable. In the bellow example I render a view with pass data you can see how I did:

Step 1: Download Laravel


Let us begin the tutorial by installing a new laravel application. if you have already created the project, then skip following step.

composer create-project laravel/laravel example-app

Step 2: Add Route

In this step,you can create route file in laravel.

routes/web.php

<?php

use App\Http\Controllers\ViewRenderController;

/*

|--------------------------------------------------------------------------

| Web Routes

|--------------------------------------------------------------------------

|

| Here is where you can register web routes for your application. These

| routes are loaded by the RouteServiceProvider within a group which

| contains the "web" middleware group. Now create something great!

|

*/

Route::get('view', [ViewRenderController::class, 'view']);

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

Step 3: Add Controller

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

php artisan make:controller ViewRenderController

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class ViewRenderController extends Controller

{

/**

* Write code on Method

*

* @return response()

*/

public function view()

{

return view('view');

}

/**

* Write code on Method

*

* @return response()

*/

public function viewRender(Request $request)

{

$viewRender = view('viewRend')->render();

return response()->json(array('success' => true, 'html'=>$viewRender));

}

}

Step 4: Add Render File

<!DOCTYPE html>

<html>

<head>

<title>Laravel 9 View Render Example - Nicesnippest.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 9 View Render Example - Nicesnippest.com</h3>

</div>

</div>

</div>

</body>

</html>

Step 5: Add Blade File

<!DOCTYPE html>

<html>

<head>

<title>Laravel 9 View Render Example - Nicesnippest.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>

Run Laravel App:

All steps have been done, now you have to type the given command and hit enter to run the laravel app:

php artisan serve

Now, you have to open web browser, type the given URL and view the app output:

http://localhost:8000/view

It will help you...

#Laravel 9