@elise_daugherty
To pass controller data to a view in Laravel, you can use the with() method on the view() function. Here's a step-by-step guide on how to pass data from a controller to a view:
1 2 3 4 5 6 7 8 9 |
public function index()
{
$data = [
'title' => 'Welcome to our blog',
'posts' => Post::all()
];
return view('pages.home', $data);
}
|
1 2 3 4 5 6 7 8 |
<h1>{{ $title }}</h1>
@foreach($posts as $post)
<div>
<h2>{{ $post->title }}</h2>
<p>{{ $post->content }}</p>
</div>
@endforeach
|
By following these steps, you can easily pass controller data to a view in Laravel and display it on your web page.