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

Shivamyadav's avatar

why i am getting the value of slug and date null , while using yamlfontmatter?

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>
0 likes
3 replies
tykus's avatar

Because you're assigning to something completely different here $this->$date and here $this->$slug

    public function __construct($title, $date, $excerpt, $body, $slug)
    {
        $this->title = $title;
        $this->date = $date; // not $this->$date
        $this->excerpt = $excerpt;
        $this->body = $body;
        $this->slug = $slug; // not $this->$slug
    }
1 like
Shivamyadav's avatar

@tykus why i am getting this url /posts/ in the browser when i have used slug

<a href="/posts/{{ $post->slug }}"> {{ $post->title }} </a>
tykus's avatar
tykus
Best Answer
Level 104

@Shivamyadav because $post->slug is null

If you assign to $this->$slug in the constructor, you are not assigning to the public $slug property; but you are dynamically assigning to a property named for whatever the value of $slug has in the constructor.

Please or to participate in this conversation.