How to pass data to view in Laravel

Laravel provides different way to pass data in view files. Data can be passed to view either from controller or directly from route in web file. For simpler view where not much calculation is needed on data part , data can be passed from web file . But with most of the applications, calculations are required on DB data part before passing to view in theses cases we use controller methods to pass data to view. Laravel provides different ways to pass complex data like array/collections etc. from controller  to view.

Lets discuss different methods in details.

  • Using view ()
  • Using with()
  • Using compact()

1. Using view() :

Data can be passed to view in key-value pair as second parameter like below:

Write the below code in the web.php file.

Livewire Component Library

Route::get('/', function () {
    $adds = ['add 1','add 2','add 3'];
    return view('info', ['name' => 'anaa', 'adds' => $adds]);
});
In above code the first parameter is name of view file and the second parameter is data in key-value pair and array.

 

Host Laravel Application on DigitalOcean

Use coupon 5balloons on this Cloudways Affiliate URL to get special discount.

You can create and write the below code in the info.blade.php file in resources/views directory.


<html>
    <body>
        <h1>Hello,  {{$name}} </h1>
        <ul>
            @foreach ($adds as $add)
                <li>{{ $add }}</li>
            @endforeach
        </ul>

    </body>
</html>
Output would be :

2. Using with :

As an alternative to passing a complete array of data to the view helper function, you may use the with method to add individual pieces of data to the view.  We can send array and any form of data using with method like below :

Write the below code in the web.php file.


 Route::get('/', function () {
   $name = "anna";
   $adds = ['add 1','add 2','add 3'];
    return view('info')->with('name',$name)
->with('adds',$adds);

  });

Create and write the below code in the ‘info.blade.php’ file in ‘resources/views’ directory.


<html>
    <body>
        <h1>Hello,  {{$name}} </h1>
        <ul>
            @foreach ($adds as $add)
                <li>{{ $add }}</li>
            @endforeach
        </ul>

    </body>
</html>

Output would be same for all three methods:

3.Using compact():

The compact is a PHP function that can be used for creating an array with variable and there value. Here the name on the variable is the key and the variable content is the value. so this can be used with any form of data and used extensively with complex application.
Below is a example of using compact():
Write the below code in the web.php file.


 Route::get('/', function () {
   $name = "anna";
   $adds = ['add 1','add 2','add 3'];
    return view('info',compact('name','adds'));

 });

 

Create and write the below code in the info.blade.php file in resources/views directory.


<html>
    <body>
        <h1>Hello,  {{$name}} </h1>
        <ul>
            @foreach ($adds as $add)
                <li>{{ $add }}</li>
            @endforeach
        </ul>

    </body>
</html>

Output :

So we have discussed the different methods of passing data to view. Same thing can be done in controller instead of web file .
In that case we have to specify the route with appropriate controller method like below:

Route::get('/', 'InfoController@showInfo');

In InfoController.php kept under app/Http/Controllers directory, we will create a public function name  showInfo


<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
  
class InfoController extends Controller
{
    public function showInfo() {
      $adds = ['add 1','add 2','add 3'];
      return view('info', ['name' => 'anaa', 'adds' => $adds]);
    }
}

View file will be the same as before , now data is passed to view in Controller where appropriate controller method is specified in web.php in respective route.

Site Footer