gpt4 book ai didi

php - 尝试读取数组上的属性 "title"(错误异常)Laravel Php

转载 作者:行者123 更新时间:2023-12-01 23:20:00 25 4
gpt4 key购买 nike

when I try to run the following it gives the Error Exception where it can't read the title on array. Can you let me know how to fix this?

**我来自应用程序/模型的邮政编码,如下所示**

 <?php


namespace App\Models;

use Illuminate\Database\Eloquent\ModelNotFoundException; //
use Illuminate\Support\Facades\File;

class Post
{
public $title;

public $excerpt;

public $date;

public $body;

public function __construct($title, $excerpt, $date, $body)
{
$this-> title = $title; //this is where the error is occurring
$this-> excerpt = $excerpt;
$this-> date = $date;
$this-> body = $body;
}

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

return array_map(function ($file){
return $file-> getContents();
}, $files);


}

public static function find($slug)
{

base_path();

if (!file_exists($path = resource_path("postss/{$slug}.html"))) {
throw new ModelNotFoundException();
}

return cache()-> remember("posts.{$slug}", 7 , function () use ($path){

return file_get_contents($path);

});






}


}

我的路线类代码是

Route::get('/', function () {

$files = File::files(resource_path("postss"));
$posts =[];

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

The code is right as far as i know, I just don't know if there is something I need to add or do something to make the error go away.

最佳答案

我认为错误真的在这里

foreach ($files as $file) {
// you load an array here !!!
$document[] = YamlFrontMatter::parseFile($file);
$posts[]= new Post(
$document->title, // then you use $document as a scalar?
$document->excerpt,
$document->date,
$document->body()
);
}

我想你可能需要做

foreach ($files as $file) {
// you load an array here !!!
$document = YamlFrontMatter::parseFile($file);
$posts[]= new Post(
$document->title,
$document->excerpt,
$document->date,
$document->body()
);
}

关于php - 尝试读取数组上的属性 "title"(错误异常)Laravel Php,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68223767/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com