How to pass data to view in Laravel?

Sam Pettersson picture Sam Pettersson · Aug 20, 2013 · Viewed 222.8k times · Source

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?

Answer

Mostafa Elgaafary picture Mostafa Elgaafary · Aug 20, 2013

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

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