douguangxiang0363 2017-06-22 18:40
浏览 162
已采纳

使用Laravel的魔术方法来传递数据来查看的好处?

What does this magic do?

public function index()
{
    $products = Product::all();

    return view('products.index')->withProducts($products);
}

There is the View::make way, which takes an array as a second parameter and there is the with() way, which takes string or an array.

with('products', $products);

or

with(array('products' => $products, 'foo' => $bar));

What is the purpose of withModel($model) resp. withModels($models)?
Are there any benefits using this approach over the other opportunities?
Also related to refactoring existing model names later on?

  • 写回答

1条回答 默认 最新

  • ds122455 2017-06-22 21:52
    关注

    All the different approaches accomplish the same feature, and it's a good example of Laravel's flexibility, and extensive use of magic methods.

    For example, using:

    return view('some-view')->withFooBar('some great value');
    

    ..is only a "faster" way to pass along a variable named foo_bar (snake cased, code here) with some specific value. This could be useful when passing along one or a few (simple) variables, but it's up to you really!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献