How to Pass Data from Controller to View in 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...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- How to use Carbon in Laravel Blade or Controller File?
- How to Call Controller Function in Blade Laravel?
- Laravel 9 Clear Cache of Route, View, Config, Event Commands
- Laravel Blade Include File If Exists Example
- Laravel Include Blade File with Data Example
- How to Use MySQL View in Laravel?
- Laravel - How to Check If Array is Empty in Blade?
- Laravel Clear Cache of Route, View, Config Command
- Laravel - How to Get .env Variable in Blade or Controller?
- Laravel Generate PDF from HTML View File and Download Example
- How to use Inject View in Laravel?
- Laravel Ajax Render View With Data Example
- Laravel Blade Check if View Exists or Not Example