Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Returning multiple variables with view::share( ) - Laravel 5.1

I want to return multiple variable to my view.

$currentUser = Auth::user();
$needToBePassed = "Lorem Ipsum"
View::share ( 'currentUser', $currentUser);

This code works fine, however how can if I also want to share $needToBePassed, what should I do?

Is rewriting it is a good practice?

View::share ( 'currentUser', $currentUser);
View::share ( 'needToBePassed', $needToBePassed);
like image 717
senty Avatar asked Oct 04 '15 23:10

senty


2 Answers

You can pass an array of data,

$data = array(
    'currentUser' => $currentUser,
    'needToBePassed' => $needToBePassed,
);
View::share('data', $data);
like image 102
Federkun Avatar answered Oct 23 '22 10:10

Federkun


I know the question is already answered, but maybe I can still help a few people by adding this solution. This way you don't have to change all your views.

I was looking for a way to solve this issue without changes all my views.

$currentUser = Auth::user();
$needToBePassed = "Lorem Ipsum";

View::share(['currentUser' => $currentUser, 'needToBePassed' => $needToBePassed]);
like image 8
KB2497 Avatar answered Oct 23 '22 11:10

KB2497