How to Pass Data from Controller to View in Laravel?

By Hardik Savani April 16, 2024 Category : Laravel

Hi Friends,

In this quick guide, we will teach you how to pass data from controller to view in laravel. I would like to show you laravel pass data from controller to view. you can see how to pass array data from controller to view in laravel. We will use how to pass multiple data from controller to view in laravel. Here, Create a basic example of how to pass variable value from controller to view in laravel.

You can use this example with laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11 versions.

Laravel provides a way to pass data from the controller to the blade file. I will give you the following three ways to pass data from controller to view. so, let's see the following examples:

1) Example 1: Using Compact()

2) Example 2: Using Array

3) Example 3: Using With()

let's see both examples with output:

Example 1: Using Compact()

app/Http/Controllers/DemoController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class DemoController extends Controller

{

/**

* Write code on Method

*

* @return response()

*/

public function index(Request $request)

{

$title = "Welcome to ItSolutionStuff.com";

$subTitle = "Thank you";

return view('demo', compact('title', 'subTitle'));

}

}

resources/views/demo.blade.php

<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8">

<meta name="viewport" content="width=device-width, initial-scale=1">

<title></title>

</head>

<body>

<h1>{{ $title }}</h1>

<h2>{{ $subTitle }}</h2>

</body>

</html>

Example 2: Using Array

app/Http/Controllers/DemoController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class DemoController extends Controller

{

/**

* Write code on Method

*

* @return response()

*/

public function index(Request $request)

{

$title = "Welcome to ItSolutionStuff.com";

$subTitle = "Thank you";

return view('demo', [

'title' => $title,

'subTitle' => $subTitle,

]);

}

}

resources/views/demo.blade.php

<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8">

<meta name="viewport" content="width=device-width, initial-scale=1">

<title></title>

</head>

<body>

<h1>{{ $title }}</h1>

<h2>{{ $subTitle }}</h2>

</body>

</html>

Example 3: Using With()

app/Http/Controllers/DemoController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class DemoController extends Controller

{

/**

* Write code on Method

*

* @return response()

*/

public function index(Request $request)

{

$title = "Welcome to ItSolutionStuff.com";

$subTitle = "Thank you";

return view('demo')

->with('title', $title)

->with('subTitle', $subTitle);

}

}

resources/views/demo.blade.php

<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8">

<meta name="viewport" content="width=device-width, initial-scale=1">

<title></title>

</head>

<body>

<h1>{{ $title }}</h1>

<h2>{{ $subTitle }}</h2>

</body>

</html>

You can see the below output:

I hope it can help you...

Tags :
Shares