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?
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!