Home Laravel How to Pass Data from Controller to View in Laravel?

How to Pass Data from Controller to View in Laravel?

by Shahriar Sagor

Hello dev’s today now in this quick guide, i will teach you how we can pass data from controller to view in laravel. Here I would like to show you how to laravel pass data from controller to view. We can see how to pass an array data from controller to view in laravel. I will use how to pass the multiple data from controller to view in the laravel. Here, i will Create a basic example of how to pass the variable value from the controller to view in laravel application.

We can also use this example with laravel 6, laravel 7, laravel 8 and laravel 9 any versions.

Laravel provides a way to pass the data from the controller to the blade file. So here I will give you the following three ways to pass the 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()

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 Codingspoint.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 Codingspoint.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 Codingspoint.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>

Read Also: How To Create Middleware In Laravel 9 ?

Thanks for read. I hope it help you. For more you can follow us on facebook

close

You may also like