Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

alex_250ug's avatar

Undefined variable $posts

Hello I'm learning Laravel from this site (Laravel 8 from scratch) and I have must made a mistake somewhere when watchin 12/13th episode (I've been stuck with this error for way too long and I know it's some stupid mistake but please help me out)

My web.php

<?php

use Illuminate\Support\Facades\Route;
use App\Models\Post;
use Spatie\YamlFrontMatter\YamlFrontMatter;

Route::get('/', function () {
    $files = File::files(resource_path("posts"));
    $posts = [];

    foreach ($files as $file) {
        $document = YamlFrontMatter::parseFile($file);
        

        $posts[] = new Post(
        $document->title,
        $document->excerpt,
        $document->date,
        $document->body(),
        $document->slug
        );
    }
    
    return view('posts', [
   'posts' => $posts
    ]);
});

Route::get('posts/{post}', function ($slug) {
    return view ('posts', [
        'post' => Post::find($slug)
  ]);
})->where('post', '[A-z_\-]+');

My post.php

<?php

namespace App\Models;

use Illuminate\Support\Facades\File;

class Post
{
    public $title;

    public $excerpt;

    public $date;
    
    public $body;
    
    public $slug;

    public function __construct($title, $excerpt, $date, $body, $slug)
    {
        $this->title = $title;
        $this->excerpt = $excerpt;
        $this->date = $date;
        $this->body = $body;
        $this->slug = $slug;
    }
    
    
    public static function all()
    {
        $files = File::files(resource_path("posts/"));

        return array_map(fn($file) => $file->getContents(), $files);
    }



    public static function find($slug)
    {
        if (! file_exists($path = resource_path("posts/{$slug}.html"))) {
            throw new ModelNotFoundException();

        }

        return cache()->remember("posts.{$slug}", 3600, fn() => file_get_contents($path));

    }

}

And posts.blade.php

<!doctype html>

<title>Blogísek</title>
<link rel="stylesheet" href="/app.css">

<body>
    <?php foreach ($posts as $post) : ?>
        <article>
            <h1>
                <a href="/posts/<?= $post->slug; ?>">
                <?= $post->title; ?>
        </a>
    </h1>

    <div>
        <?= $post->excerpt; ?>
    </div>
</article>
<?php endforeach; ?>

</body>
0 likes
4 replies
Snapey's avatar

format your code blocks by three backticks ``` on their own line before and after the code

You can go back and edit your original question.

You say you follow the tutorial, why not using blade formatting?

Snapey's avatar
Snapey
Best Answer
Level 122

you are returning the same view for both the index and the show methods. When used from the show method, you pass $post to the view and not $posts.

Change the show method to use your post view

alex_250ug's avatar

@Snapey Thank you so much I've changed return view ('posts', [ to return view ('post', [ and it works fine but only on my 3rd post and 4th post when I want to open 1st/2nd post on my blog it shows up 404 not found.. Any idea why?

Snapey's avatar

@alex_250ug if it fails on certain posts then maybe there is a problem with the integrity of the data rather than the code,

Please or to participate in this conversation.