ItSolutionStuff.com

How to Pass Data from Controller to View in Laravel?

By Hardik Savani • April 16, 2024
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: Laravel
Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

How to use Carbon in Laravel Blade or Controller File?

Read Now →

How to Call Controller Function in Blade Laravel?

Read Now →

Laravel 9 Clear Cache of Route, View, Config, Event Commands

Read Now →

Laravel Blade Include File If Exists Example

Read Now →

Laravel Include Blade File with Data Example

Read Now →

How to Use MySQL View in Laravel?

Read Now →

Laravel - How to Check If Array is Empty in Blade?

Read Now →

Laravel Clear Cache of Route, View, Config Command

Read Now →

Laravel - How to Get .env Variable in Blade or Controller?

Read Now →

Laravel Generate PDF from HTML View File and Download Example

Read Now →

How to use Inject View in Laravel?

Read Now →

Laravel Ajax Render View With Data Example

Read Now →

Laravel Blade Check if View Exists or Not Example

Read Now →