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?
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
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>
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
Please or to participate in this conversation.