You can also pass an array as the second argument after the view template name, instead of stringing together a bunch of ->with()
methods.
return View::make('blog', array('posts' => $posts));
Or, if you're using PHP 5.4 or better you can use the much nicer "short" array syntax:
return View::make('blog', ['posts' => $posts]);
This is useful if you want to compute the array elsewhere. For instance if you have a bunch of variables that every controller needs to pass to the view, and you want to combine this with an array of variables that is unique to each particular controller (using array_merge
, for instance), you might compute $variables
(which contains an array!):
return View::make('blog', $variables);
(I did this off the top of my head: let me know if a syntax error slipped in...)
return View::make('blog',compact('posts'));
– Decenniumarray('posts' => $posts)
, or you can use thewith()
method as shown in the accepted answer. – Stockjobberreturn view('blog',compact('posts'));
– Overreach