使用 view::share() 返回多个变量 - Laravel 5.1

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32939485/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 12:26:57  来源:igfitidea点击:

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

phplaravelblade

提问by senty

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?

这段代码工作正常,但是如果我也想分享$needToBePassed怎么办,我该怎么办?

Is rewriting it is a good practice?

重写它是一个好习惯吗?

View::share ( 'currentUser', $currentUser);
View::share ( 'needToBePassed', $needToBePassed);

采纳答案by Federkun

You can pass an array of data,

您可以传递一组数据,

$data = array(
    'currentUser' => $currentUser,
    'needToBePassed' => $needToBePassed,
);
View::share('data', $data);

回答by KB2497

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]);