dimanche 4 novembre 2018

"Undefined variable: post" when attempting to use @foreach in a blade template on a fresh install of Laravel 5.7

As the title says, when I try to use @foreach($posts as $post), I get an error that $post is undefined.

I'm guessing I've probably made some simple mistake or something like that, but I haven't been able to figure out what the problem is, so I'm asking here.

It's a pretty fresh install of Laravel 5.7.

My web.php looks like this.

Route::get('/', function () {
    $posts = App\Post::all();
    return view('landing', compact('posts'));
});

I've got a landing.blade.php file in resources/views which includes a partial using @include('partials.posts'). In resources/views/partials/ there's a posts.blade.php which has this.

$foreach($posts as $post)
    
$endforeach

And when I navigate to / in my browser, I get a Undefined variable: post error where it highlights <?php echo e($post->title); ?>, which correlates to .

If I put <?php dd($posts) ?> before the @foreach, then it returns a collection with an array of my dummy posts, so it does seem like my posts are being passed to the view.

The results of dd($posts)



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire