Installation
composer require flynsarmy/db-blade-compiler
Publish the config (if needed):
php artisan vendor:publish --provider="Flynsarmy\DbBladeCompiler\DbBladeCompilerServiceProvider" --tag="config"
Basic Usage
Define a Blade template in a model field (e.g., description in a Post model):
// Migration
Schema::table('posts', function (Blueprint $table) {
$table->text('description')->nullable();
});
Store Blade syntax in the field:
<h1>{{ $title }}</h1>
<p>Published on {{ $created_at->format('M d, Y') }}</p>
Compile and Render
Use the renderBlade() method on the model:
$post = Post::find(1);
$rendered = $post->renderBlade('description', [
'title' => 'My Awesome Post',
]);
echo $rendered;
View the Source
Check the config file for default settings (e.g., cache_path, view_paths).
Use renderBlade() to dynamically render fields with data from other fields or external sources:
$post = Post::find(1);
$html = $post->renderBlade('content', [
'author' => $post->user->name,
'tags' => $post->tags->pluck('name'),
]);
Store common Blade snippets in a dedicated table (e.g., template_snippets) and reference them via IDs:
// Migration
Schema::create('template_snippets', function (Blueprint $table) {
$table->id();
$table->text('blade_content');
$table->string('name');
});
// Usage
$snippet = TemplateSnippet::where('name', 'user_card')->first();
$userHtml = $user->renderBladeFromString($snippet->blade_content, ['user' => $user]);
Enable caching in config/db-blade-compiler.php:
'cache' => [
'enabled' => true,
'path' => storage_path('framework/views'),
],
Clear cache when templates change:
php artisan view:clear
Return rendered Blade as part of API responses:
return response()->json([
'post' => [
'title' => $post->title,
'rendered_content' => $post->renderBlade('content'),
],
]);
Use Blade conditionals to toggle content:
@if($is_premium)
<div class="premium-badge">⭐ Premium Content</div>
@endif
Create custom Blade directives for reusable logic:
// In a service provider
Blade::directive('formatDate', function ($expression) {
return "<?php echo ($expression)->format('F j, Y'); ?>";
});
// Usage in DB field
<p>Created: @formatDate($created_at)</p>
Cache Invalidation
php artisan view:clear) after updating Blade templates in the DB.Variable Scope Leaks
renderBlade().Performance Overhead
remember() method.SQL Injection Risks
{{ $user_input }}).|e or {{ __($var) }}.Missing Dependencies
public function getRenderedDescriptionAttribute()
{
return $this->renderBlade('description', [
'title' => $this->title ?? 'Untitled',
]);
}
Check Compiled Output:
Enable Blade debugging in config/app.php:
'debug' => env('APP_DEBUG', true),
View compiled views in storage/framework/views.
Log Errors:
Wrap renderBlade() calls in try-catch:
try {
$html = $model->renderBlade('field');
} catch (\Exception $e) {
Log::error("Blade render failed: " . $e->getMessage());
$html = '<p class="error">Failed to render content.</p>';
}
Test Locally:
Use tinker to test Blade rendering:
php artisan tinker
>>> $post = App\Models\Post::find(1);
>>> $post->renderBlade('description', ['title' => 'Test']);
Custom Storage Engines
Override the Flynsarmy\DbBladeCompiler\Engines\EngineResolver to support non-DB storage (e.g., Redis, S3).
Blade Extensions Register custom Blade components/directives in a service provider:
Blade::component('alert', AlertComponent::class);
Model Observers Automatically render fields on save:
class PostObserver
{
public function saved(Post $post)
{
$post->renderBlade('teaser', ['title' => $post->title])->save();
}
}
API for Template Management Build a CRUD interface for managing Blade templates in the DB via Laravel Nova or Filament.
Fallback Content Provide default Blade templates if a field is empty:
$html = $post->renderBlade('description', [
'title' => $post->title,
], '<p>No description available.</p>');
How can I help you explore Laravel packages today?