I installed the spatie/yaml-front-matter package into my Laravel project to access metadata in my HTML files. Unfortunately, I cannot sort the files by date because the date property in the metadata is returning a null value. My operating system is Microsoft Windows 10 Pro Version 10.0.19042 Build 19042. I am using Laravel version 9 and PHP Version 8.0.
Here's a copy of the metadata as it appears at the top of my HTML files.
---
title: My Fifth Post
slug: my-fifth-post
excerpt: Lorem Ipsum is simply dummy text of the printing and typesetting industry.
date: 2022-01-25
---
Below is a copy of the POST class in my Models directory.
namespace App\Models;
use Illuminate\Support\Facades\File;
use Spatie\YamlFrontMatter\YamlFrontMatter;
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()
{
return collect(File::files(resource_path("posts")))
->map(fn($file) => YamlFrontMatter::parseFile($file))
->map(fn($document) => new Post(
$document->title,
$document->excerpt,
$document->date,
$document->body(),
$document->slug
));
}
public static function find($slug)
{
return static::all()->firstWhere('slug', $slug);
}
}
Finally, a screenshot of a var_dump(Post::find('my-fifth-post')).