Home >>Laravel Tutorial >Passing Data to Laravel Views

Passing Data to Laravel Views

Passing Data to Laravel Views

Check out the Views in Laravel before moving into this section.

There are Three methods in which data can be passed into the views:

  1. Using name array
  2. Using with() function
  3. Using compact() function

1. Name Array

As view() method requires two parameters to be passed. Array of data is passed as a second parameter to view() method.

Shown below is example of name array:
  • Step 1 : Create a file for a view of th page.(Check how to create views in Laravel in Views in Laravel Section)
  • Let's create names.blade.php

    	<html> 
    	 <body>  
    	 <h1> Name Array View : 
    		<br> 
    		<?php   
    		echo $n1;  
    		echo "<br>";  
    		echo $n2;  
    		echo "<br>";  
    		echo $n3; 
    		?>
    	 </h1>   
    	 </body>   
    	</html>   
    

    Above view displays the values n1, n2, n3 variables.


  • Step 2 : Create NameController.php file, from here variables n1, n2, n3 will retrieve the values.
  • <?php  
    namespace App\Http\Controllers;  
    use Illuminate\Http\Request;  
    class NameController extends Controller  
    {  
       public function show()  
      {  
         return view('names',['n1'=> 'Yanik','n2'=>'Nonu','n3'=>'Believe Master']);  
      }   
    }  
    

    Above controller's show() method is defined to return the view of name.blade.php

  • Step 3: Make route in web.php file.
  • Route::get('/names', 'NameController@show');  
    
Output:
Display Data on View

2. with() Function

with() function lets the controller to take the value of 'id' along with the view it is returning to display view with data.

Shown below is example of with() function:
  • Step 1: Make simple view with.blade.php containg following view.
  • <html> 
     <body>  
     <h1> Student id is :   
     <?php   
     echo $id;  
     ?>  
    </body> 
    </html>   
    

    Above code will display '$id' value

  • Step 2 : Create a WithController.php file with following code.
  • <?php  
    namespace App\Http\Controllers;  
    use Illuminate\Http\Request;  
    class WithController extends Controller  
    {  
      public function show($id)  
      {  
        return view('student')->with('id',$id);  
      }   
    }  
    

    Above controller method show() is passed with a parameter $id which then returns the view of with.blade.php along with the value of id we passed using with() function. The 'with()' function contains two parameters,name of variable and value of that variable in this case 'id'.

  • Step 3 : Create a route for URL
  • Route::get('/with/{id}','WithController@show');  
    
Output:
Display Data on View using with

3. compact() Function

Compact function in comparision to with function which contains only two parameters, compact function can have multiple parameters.

Shown below is example of compact() function
  • Step 1: Create a blade file containg view of the page. In this case 'compact.blade.php' with following code.
  • <html>
     <body>  
     <h1>Name using compact() function is :   
     <?php   
     echo $n;?>
    </body>  
    </html>  
    
  • Step 2: Create a controller with a method show() as shown below a 'CompactController.php'
  • <php  
    namespace App\Http\Controllers;  
    use Illuminate\Http\Request;  
    class CompactController extends Controller  
    {  
        public function show($n)  
      {  
        return view('student', compact('n'));  
      } }  
    
  • Step 3:At last define a route to get the URL.
  • Route::get('/compact/{n}', 'CompactController@show');  
    
Output:
Display Data on View using compact

Pass Multiple parameters in compact() function.

Shown below some code as an example for multiple parameters in compact() function.

Here is compact.blade.php

<html>  
 <body>    
 <h1>  Compact() method Name Details : <br>     
 <?php   
 echo "id is :" .$id;  
 echo "<br>";  
 echo "name is :" .$n;  
 echo "<br>";  
 echo "password is :" .$p; ?></h1>     
</body>  
</html>   

Here is CompactController.php

<?php  
namespace App\Http\Controllers;  
use Illuminate\Http\Request;  
class CompactController extends Controller  
{  
   public function show($id,$n,$p)  
  {  
     return view('student',compact('id','n','p'));  
  }   
}  

Here is Route(web.php)

Route::get('/details/{id}/{n}/{p}', 'CompactController@show');
Output:
Display Data on blade

No Sidebar ads