Well, I got it to work - the body element needed to be ended with prentices.
Nov 28, 2022
4
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 ▶}
]
Level 10
Please or to participate in this conversation.