2017-06-22 66 views
0

這是什麼魔力呢?優勢利用Laravel對通數據魔術方法來查看?

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

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

還有就是View::make方式,這需要一個數組作爲第二個參數並且在with()方式,這需要串或陣列。

with('products', $products); 

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

什麼是withModel($model) RESP的目的。 withModels($models)
是否有使用這種方法比其他機會什麼好處?
還關係到以後重構現有模型的名字呢?

回答

0

所有不同的方法完成同樣的功能,並且它是Laravel的靈活性和廣泛使用的魔術方法一個很好的例子。

例如,使用:

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

..是隻有一個「快」的方式沿(code here蛇套管,)與一些特定的值命名爲foo_bar一個變量傳遞。這在傳遞一個或幾個(簡單)變量時很有用,但這取決於你!