PHP code example of jcfrane / laravel-md-blog

1. Go to this page and download the library: Download jcfrane/laravel-md-blog library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

jcfrane / laravel-md-blog example snippets


use JCFrane\MdBlog\Facades\MdBlog;

// All published posts
$posts = MdBlog::all();

// Single post by slug
$post = MdBlog::find('my-first-post');

// Filter by tag
$posts = MdBlog::whereTag('laravel');

// Filter by category
$posts = MdBlog::whereCategory('tutorials');

// Published posts, newest first
$posts = MdBlog::latest();

// Paginated, newest first (page read from the request like Eloquent)
$posts = MdBlog::paginate(9);

// Clear the cache
MdBlog::clearCache();

MdBlog::query()
    ->whereTag(['sleep', 'focus'])      // any of these tags
    ->whereAllTags(['sleep', 'science']) // every one of these tags
    ->whereCategory('guides')            // string or array
    ->whereMeta('author', 'JC')          // extra front matter; one arg checks the key exists
    ->whereYear(2026)
    ->search('white noise')              // case-insensitive over title, excerpt, tags, body
    ->where(fn (Post $post) => $post->readingTime <= 5)
    ->latest()                           // or ->oldest(), ->orderBy('title'), ->orderBy(fn ($p) => ..., 'desc')
    ->skip(10)->take(5)
    ->get();                             // ->first(), ->count(), ->exists()

return Inertia::render('Blog/Index', [
    'posts' => MdBlog::query()->whereTag($tag)->paginate(9),
]);

$post = MdBlog::find($slug);

MdBlog::previous($post); // older post or null
MdBlog::next($post);     // newer post or null
MdBlog::related($post);  // up to 3 posts sharing tags or category, best match first
MdBlog::related($post, 6);

$post->title;        // string
$post->slug;         // string
$post->date;         // Carbon instance
$post->body;         // raw markdown
$post->html;         // rendered HTML
$post->tags;         // array
$post->category;     // string
$post->excerpt;      // string (front matter or generated)
$post->cover;        // string|null (resolved URL)
$post->readingTime;  // int, minutes
$post->published;    // bool
$post->meta;         // array (extra front matter fields)
$post->filePath;     // string
$post->lastModified; // int (unix timestamp)

// In an Inertia controller
return Inertia::render('Blog/Show', [
    'post' => MdBlog::find($slug),
]);

// config/md-blog.php
'post_class' => App\Blog\CustomPost::class,

namespace App\Blog;

use JCFrane\MdBlog\Post;

class CustomPost extends Post
{
    public static function make(array $attributes): static
    {
        // Transform the rendered HTML before readonly properties are set
        $attributes['html'] = \Spatie\ShikiPhp\Shiki::highlight($attributes['html']);

        return parent::make($attributes);
    }
}

'images' => [
    'enabled'   => true,
    'cache_ttl' => 86400, // in seconds
],

use JCFrane\MdBlog\Contracts\EmailRecipient;
use JCFrane\MdBlog\Traits\ReceivesPostMail;
use JCFrane\MdBlog\Post;

class User extends Authenticatable implements EmailRecipient
{
    use ReceivesPostMail;

    public function shouldReceivePostEmail(Post $post): bool
    {
        return $this->subscribed_to_newsletter;
    }
}

// Facade
MdBlog::sendPost('resources/markdown/blog/my-post.md');

// config/md-blog.php
return [
    'path' => env('MD_BLOG_PATH', 'resources/markdown/blog'),

    'route_prefix' => env('MD_BLOG_ROUTE_PREFIX', 'md-blog'),

    // Base URL for image and cover URLs; null keeps them root-relative.
    'url' => env('MD_BLOG_URL', null),

    'post_class' => null, // e.g. App\Blog\CustomPost::class

    'excerpt_length'   => env('MD_BLOG_EXCERPT_LENGTH', 160),
    'words_per_minute' => env('MD_BLOG_WORDS_PER_MINUTE', 200),

    'cache' => [
        'enabled' => env('MD_BLOG_CACHE_ENABLED', true),
        'store'   => env('MD_BLOG_CACHE_STORE', null), // null = app default
        'ttl'     => env('MD_BLOG_CACHE_TTL', 3600),
    ],

    'images' => [
        'enabled'   => env('MD_BLOG_IMAGES_ENABLED', true),
        'cache_ttl' => env('MD_BLOG_IMAGES_CACHE_TTL', 86400),
    ],

    'commonmark' => [
        'html_input'         => 'strip',
        'allow_unsafe_links' => false,
    ],

    'mail' => [
        'enabled'          => env('MD_BLOG_MAIL_ENABLED', false),
        'recipient_model'  => env('MD_BLOG_MAIL_RECIPIENT_MODEL', null),
        'queue'            => env('MD_BLOG_MAIL_QUEUE', false),
        'queue_connection' => env('MD_BLOG_MAIL_QUEUE_CONNECTION', null),
        'queue_name'       => env('MD_BLOG_MAIL_QUEUE_NAME', null),
        'chunk_size'       => env('MD_BLOG_MAIL_CHUNK_SIZE', 50),
        'middleware'       => ['auth'],
        'subject_prefix'   => env('MD_BLOG_MAIL_SUBJECT_PREFIX', ''),
    ],
];
bash
php artisan vendor:publish --tag=md-blog-config
bash
php artisan vendor:publish --tag=md-blog-views
bash
php vendor/bin/testbench serve