📜  用于平面文件 CMS 的 laravel Post 模型 - PHP 代码示例

📅  最后修改于: 2022-03-11 14:54:35.536000             🧑  作者: Mango

代码示例1
rememberForever('posts.all', function () {
            return collect(File::files(resource_path('posts')))
            ->map(fn ($file) => YamlFrontMatter::parseFile($file))
            ->map(fn ($doc) => new Post(
                    $doc->title,
                    $doc->excerpt,
                    $doc->date,
                    $doc->body(),
                    $doc->slug
            ))
            ->sortByDesc('date');
        });
    }

    public static function find($slug)
    {
        return static::all()->firstWhere('slug', $slug);
    }

    public static function findOrFail($slug)
    {
        $post = static::find($slug);
        if (! $post) {
            throw new ModelNotFoundException();
        }
        return $post;
    }
}