How To Pass Data To Views from controller In Laravel?

Today, We want to share with you laravel pass data to view from controller.In this post we will show you sharing data with all views laravel, hear for how to pass array from controller to view in laravel? we will give you demo and example for implement.In this post, we will learn about Laravel Passing Multiple Variables From Controller To View with an example.

how to pass data from one view to another view in laravel?

There are various ways of passing data to views:

  • By using the name array
  • By using with() function
  • By using compact() function

using Name array

member.blade.php

<html>  
 <body>  
 <h1>Name of the Members are : <br>  
 <?php   
echo $name1;  
echo "<br>";  
echo $name2;  
echo "<br>";  
echo $name3; ?></h1>  
</body>  
</html>  

MemberController.php.

<?php  
namespace App\Http\Controllers;  
use Illuminate\Http\Request;  
class MemberController extends Controller  
{  
   public function display()  
  {  
     return view('member',['name1'=> 'Sejal','name2'=>'Pankila','name3'=>'Diopti']);  
  }   
  }  

web.php

Route::get('/profiles', '[email protected]');  

using with() function

member.blade.php

<html>  
 <body>  
 <h1>Member id is :   
 <?php   
echo $id;  
?>  
</body>  
</html>  

MemberController.php

<?php  
namespace App\Http\Controllers;  
use Illuminate\Http\Request;  
class MemberController extends Controller  
{  
  public function display($id)  
  {  
    return view('member')->with('id',$id);  
  }   
}  

using Route

Route::get('/profiles/{id}', '[email protected]');  

using compact() function

member.blade.php

<html>  
 <body>  
 <h1>Name is :   
 <?php   
echo $name;?>  
</body>  
</html>  

MemberController.php

<?php  
namespace App\Http\Controllers;  
use Illuminate\Http\Request;  
class MemberController extends Controller  
{  
    public function display($name)  
  {  
    return view('member?, compact('name'));  
  } }  

web.php file.

Route::get('/profiles/{name}', '[email protected]');  

Full Example

Member.blade.php

<html>  
 <body>  
<h1>Members Details : <br>  
<font size='5' face='Arial'>  
<?php   
echo "member id is :" .$id;  
echo "<br>";  
echo "Member name is :" .$name;  
echo "<br>";  
echo "Member password is :" .$password; ?></h1>  
</font>  
</body></html>  

MemberController.php

<?php  
namespace App\Http\Controllers;  
use Illuminate\Http\Request;  
class MemberController extends Controller  
{  
   public function display($id,$name,$password)  
  {  
     return view('member',compact('id','name','password'));  
  }   
}  

web.php
Route::get('/profiles/{id}/{name}/{password}', '[email protected]');  

I hope you get an idea about laravel return view with json data.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Read Also:  Laravel JSON Encode Decode Handling

About Pakainfo

I am Jaydeep Gondaliya, a software engineer, the founder and the person running Pakainfo. I'm a full-stack developer, entrepreneur and owner of Pakainfo.com. I live in India and I love to write tutorials and tips that can help to other artisan, a Passionate Blogger, who love to share the informative content on PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.

View all posts by Pakainfo →

Leave a Reply

Your email address will not be published. Required fields are marked *