将多个变量传递给 Laravel 中的视图?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30304405/
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
Passing multiple variables to a view in laravel?
提问by user30102967
I retrieved those from the database and assigned them to variables, but i am not able to pass both of them to the view.
我从数据库中检索了它们并将它们分配给变量,但我无法将它们都传递给视图。
$Posts = posts::find($id);
$image = images::find($id);
I tried passing them both in an array but haven't had luck. tried this as well
我试着把它们都放在一个数组中,但没有运气。也试过这个
return View::make('Index',['x_var' => $Posts, 'y_var' => $image]);
In the view it can only recognise the x_var
and when i use the y_var
the page crashes.
在视图中它只能识别x_var
当我使用时y_var
页面崩溃。
回答by vps
You can use following...
您可以使用以下...
return View::make('index', compact('Posts', 'image'));
回答by Nick
Or put it in a array like that..
或者把它放在这样的数组中..
$data = array(
'Posts' => posts::find($id),
'image' => images::find($id),
);
return View::make('Index')->with($data);
Edit
编辑
If you get an error with the y_var
maybe there is a problem with the image you want to find. Please give us more information about the error.
如果您遇到错误,y_var
可能是您要查找的图像有问题。请向我们提供有关错误的更多信息。
回答by Akar
There are two ways you can do this:
有两种方法可以做到这一点:
The first way:
第一种方式:
return View::make('view', ['Posts' => $posts, 'image', $image]);
And the second way, Which is for me, is much cleaner:
第二种方式,这对我来说,要干净得多:
return View::make('view', compact('Posts', 'image'));
And please be consistent when you're coding.
并且在编码时请保持一致。
It's really annoying to see a variable capitalized.
看到变量大写真的很烦人。