Understanding View::share in Laravel


Understanding View::share in Laravel is easy but tricky to new users. Here I will try to clear the confusions of Laravel’s View::share.

First look at the code below. We defined a ProfileController and inside it we have a public function as getIndex().

<?php 
class ProfileController extends BaseController{
	public function getIndex(){
		$profile_info = User::find('2');
		View::share('getinfo',$profile_info);
		$this->layout->content = view::make('admin/my-profile');
	}
}
?>

$Profile_info is having an array where all the user information from Users table are kept. Now we are going to share this $Profile_info array/values to our “admin/my-profile” view page.

View::share('getinfo',$profile_info);

In this line we give $Profile_info a name that is “getinfo”. Now we can get the value of $Profile_info in the “admin/my-profile” page by using $getinfo. As we are handling an array, say the first element of the getinfo array is “first_name” so we can retrieve the value like this $getinfo->first_name.

,

Leave a Reply