How to pass data to view in Laravel?

Im passing data to my blade view with return View::make('blog', $posts); and in my blade view I'm trying to run an @foreach ($posts as $post) I end up with an error saying that $posts isn't defined.

My question is how would the $posts array be called?


Solution 1:

You can pass data to the view using the with method.

return View::make('blog')->with('posts', $posts);

Solution 2:

It's probably worth mentioning that as of Laravel 5, passing data to the view is now done like this:

return view("blog", ["posts"=>$posts]);

Or:

return view("blog", compact("posts"));

Documentation is available here.