laravel compact() and -gt;with()(laravel compact() 和 -gt;with())
问题描述
我有一段代码,我试图找出为什么一个变体有效而另一个无效.
I have a piece of code and I'm trying to find out why one variation works and the other doesn't.
return View::make('gameworlds.mygame', compact('fixtures'), compact('teams'))->with('selections', $selections);
这让我可以按预期生成赛程、球队和选择的数组视图.
This allows me to generate a view of arrays for fixtures, teams and selections as expected.
然而,
return View::make('gameworlds.mygame', compact('fixtures'), compact('teams'), compact('selections'));
不允许正确生成视图.我仍然可以回显数组并获得预期的结果,但视图在到达选择部分后不会呈现.
does not allow the view to be generated properly. I can still echo out the arrays and I get the expected results but the view does not render once it arrives at the selections section.
没关系,因为我让它使用 ->with()
语法,但只是一个奇怪的语法.
It's oké, because I have it working with the ->with()
syntax but just an odd one.
谢谢.搜索引擎
推荐答案
View::make
函数采用 3 个参数,根据文档是:
The View::make
function takes 3 arguments which according to the documentation are:
public View make(string $view, array $data = array(), array $mergeData = array())
在您的情况下, compact('selections')
是 4th 参数.它不会传递给视图,并且 laravel 会抛出异常.
In your case, the compact('selections')
is a 4th argument. It doesn't pass to the view and laravel throws an exception.
另一方面,您可以使用 with()
任意次数.因此,这将起作用:
On the other hand, you can use with()
as many time as you like. Thus, this will work:
return View::make('gameworlds.mygame')
->with(compact('fixtures'))
->with(compact('teams'))
->with(compact('selections'));
这篇关于laravel compact() 和 ->with()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:laravel compact() 和 ->with()
基础教程推荐
- XAMPP 服务器不加载 CSS 文件 2022-01-01
- 如何在 PHP 中的请求之间持久化对象 2022-01-01
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01
- 在多维数组中查找最大值 2021-01-01
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01
- 超薄框架REST服务两次获得输出 2022-01-01
- Libpuzzle 索引数百万张图片? 2022-01-01