Search
 
SCRIPT & CODE EXAMPLE
 

PHP

laravel Post model for flat file CMS

<?php

namespace AppModels;

use IlluminateDatabaseEloquentModelNotFoundException;
use IlluminateSupportFacadesFile;
use SpatieYamlFrontMatterYamlFrontMatter;


class Post
{
    public function __construct(
        public string $title,
        public string $excerpt,
        public int $date,
        public string $body,
        public string $slug
    ){}

    public static function all()
    {
        return cache()->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;
    }
}
Comment

PREVIOUS NEXT
Code Example
Php :: php kril to eng 
Php :: display rows brought back by query php 
Php :: Uncaught Error: Call to undefined function "themeblvd_add_builder_element()" 
Php :: composer_memory_limit 
Php :: convert stdclass to json in php 
Php :: create custom domain in localhost xampp 
Php :: integer division in php 
Php :: update user meta wp code 
Php :: php fwrite new line 
Php :: laravel gigapay list employee 
Php :: wordpress acf get checkbox options 
Php :: laravel abort_if 
Php :: how to delete image from floder in laravel 
Php :: php check if its a name 
Php :: Google Dorks Using special search string to find vulnerable websites: 
Php :: laravel session put method 
Php :: select session php 
Php :: rus text check php 
Php :: php get version 
Php :: php 6 digit random number 
Php :: laravel gigapay create payout 
Php :: php get prameter 
Php :: isset blade laravel 
Php :: laravel vendor:publish not working 
Php :: php check if number starts with 0 
Php :: php date month italian 
Php :: fixuphost 
Php :: how to get video duration in php 
Php :: webstorm vs phpstorm 
Php :: how to document php api with swagger 
ADD CONTENT
Topic
Content
Source link
Name
6+5 =