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 怎样才能让鼠标沿着线条的中心线轨迹移动
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?