Help me sir @jeffreyway and @Sinnbeck @Snapey , Why i am getting the date and slug data null null // routes\web.php:35 this is for specific slug.
my html file code
---
title: My First Post
slug: my-first-post
excerpt: Lorem Ipsum is simply dummy text of the printing and typesetting industry.
date: 2022-10-29
---
<p>
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
</p>
my routes code
Route::get('/', function () {
$files = File::files(resource_path("posts"));
$posts = [];
foreach($files as $file)
{
$document = YamlFrontMatter::parseFile($file);
$posts[] = new Post(
$document->title,
$document->date,
$document->excerpt,
$document->slug,
$document->body(),
);
}
dd($posts[1]->slug);
return view('posts', compact('posts'));
});
my post model
<?php
namespace App\Models;
use Illuminate\Support\Facades\File;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\ModelNotFoundException;
class Post
{
use HasFactory;
public $title;
public $date;
public $excerpt;
public $body;
public $slug;
public function __construct($title, $date, $excerpt, $body, $slug)
{
$this->title = $title;
$this->$date = $date;
$this->excerpt = $excerpt;
$this->body = $body;
$this->$slug = $slug;
}
public static function allPost()
{
$files = File::files(resource_path("posts"));
return array_map(fn($file) => $file->getContents(), $files );
}
public static function find($slug)
{
$path = resource_path("posts/{$slug}.html");
if(!file_exists($path))
{
throw new ModelNotFoundException();
}
return cache()->remember("posts.{slug}", 1200, fn() => file_get_contents($path));
// return file_get_contents($path);
}
}
my blade code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<link rel="stylesheet" href="{{ url('css/app.css') }}">
</head>
<body>
@foreach ($posts as $post)
<article>
<h1>
<a href="/posts/{{ $post->slug }}"> {{ $post->title }} </a>
</h1>
<div>
{!! $post->excerpt !!}
</div>
</article>
@endforeach
</body>
</html>