Home >Backend Development >PHP Tutorial >How Do I Access Data Passed to a Laravel Blade View?

How Do I Access Data Passed to a Laravel Blade View?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-12-13 12:39:13478browse

How Do I Access Data Passed to a Laravel Blade View?

Accessing Data Passed to View in Laravel

When attempting to utilize the array $posts within a Blade view, you may encounter an error indicating that it's not defined. This occurs because the data needs to be explicitly passed to the view using the with method.

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

By using the with method, you assign the $posts array to the variable posts, making it accessible within the Blade view. You can then safely iterate over it using @foreach:

@foreach ($posts as $post)
    <!-- Your code here -->
@endforeach

The above is the detailed content of How Do I Access Data Passed to a Laravel Blade View?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn