laravel 将多个变量传递给刀片模板

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36146240/
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 13:27:22  来源:igfitidea点击:

Passing multiple variables to blade template

phplaravellaravel-5blade

提问by Sambhav Pandey

How to pass multiple variables to blade view in Laravel ?

如何将多个变量传递给 Laravel 中的刀片视图?

Say I have to pass $var1and $var2to view 'welcome'.

说我必须通过$var1$var2查看“欢迎”。

回答by Sambhav Pandey

Posting as for others it might be useful, one can use two approaches,

发布至其他人可能有用,可以使用两种方法,

withfunction, Pass them in the form of array to the view with the help of withfunction

with函数,在with函数的帮助下以数组的形式传递给视图

$data = array('var1'=>$var1, 'var2'=>$var2);
            return view('welcome')->with($data);

compactfunction, Use compactfunction to pass the data

compactfunction, 使用compact函数来传递数据

$var1= "variable 1";
$var2 = "variable 2";
return view('welcome', compact('var1', 'var2'));

And in both cases, call the variables simply by name in the view

在这两种情况下,只需在视图中按名称调用变量

{{ $var1 }} and {{ $var2 }}

{{ $var1 }} and {{ $var2 }}

P.S. In second approach variables are being simply passed inside compact with just names and without any $ sign.

PS 在第二种方法中,变量只是简单地在紧凑型内部传递,只有名称,没有任何 $ 符号。

回答by Umesh Jee

You can pass multiple variable using array data-set. Like.

您可以使用数组数据集传递多个变量。喜欢。

$data['arrData'] = array('test1'=>$val1, 'test2'=>$val3,'test3'=>$val3); return view('welcome',$data); and this value you will get easily in blade template in array format.

$data['arrData'] = array('test1'=>$val1, 'test2'=>$val3,'test3'=>$val3); 返回视图('欢迎',$数据);并且您将在刀片模板中以数组格式轻松获得此值。