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

CamKem's avatar
Level 10

Yaml front matter not passing the body to the ddd()

Hello, I need some assistance. I'm following the beginners tutorial for Laravel and up to the section on Yaml front matter.

For some reason I cannot work out, the body of the array is showing up as null, but the meta data is being passed through.

My route looks like this

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,
        );
    };

    ddd($posts);
});

And my controller class looks like this:

class Post
{

    public $title;

    public $excerpt;

    public $date;

    public $body;

    /**
     * @param $title
     * @param $excerpt
     * @param $date
     * @param $body
     */
    public function __construct($title, $excerpt, $date, $body)
    {
        $this->title = $title;
        $this->excerpt = $excerpt;
        $this->date = $date;
        $this->body = $body;
    }


    public static function all()
    {
        $files = File::files(resource_path("/posts"));

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

Can you please help me work out why the body is showing up as null in the ddd() of the array?

PS -> debug looks like this:

array:3 [▼
  0 => App\Models\Post {#275 ▼
    +title: "My First Post"
    +except: "Lorem ipsum text that I have chosen..."
    +date: 1669593600
    +body: null
  }
  1 => App\Models\Post {#276 ▶}
  2 => App\Models\Post {#287 ▶}
]
0 likes
4 replies
CamKem's avatar
CamKem
OP
Best Answer
Level 10

Well, I got it to work - the body element needed to be ended with prentices.

UmarShahid's avatar

I have this same issue I am doing venues instead of posts in web.php : Route::get('/', function () {

$files = File::files(resource_path("/venues"));
$venues = [];

foreach ($files as $file) {
    $document = YamlFrontMatter::parseFile($file);
    $venues[] = new Venue(
        $document->title,
        $document->excerpt,
        $document->date,
        $document->body
    );
};

ddd($venues[0]);

and this is my html: --- title: My Fifth Venue excerpt: Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium date: 2023-04-12

Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?"

but my body is null in ddd. Could you please help me with what am I doing wrong
UmarShahid's avatar

@CamKem Thank you its now working, I was calling venues[0]->body() instead of $document->body()

Thanks a lot

Please or to participate in this conversation.