kiroushi / laravel-db-blade
从 Eloquent 模型字段渲染 Blade 模板
0.1.0
2019-04-17 10:26 UTC
Requires
- php: ^7.2
- laravel/framework: ~5.8.0
Requires (Dev)
- phpunit/phpunit: ^8.0
This package is auto-updated.
Last update: 2024-09-08 12:22:28 UTC
README
从 Eloquent 模型字段渲染 Blade 模板
此包允许您从数据库模型而不是文件中渲染 Blade 模板。它基于 Flynsarmy 的 laravel-db-blade-compiler。
安装
您可以通过 composer 安装此包
composer require kiroushi/laravel-db-blade
使用 artisan 发布资源
php artisan vendor:publish
然后在 config/db-blade.php
配置文件中设置您的 DbView 模型。包中包含默认模型(Kiroushi\DbBlade\Models\DbView)
return [ 'model_name' => 'Kiroushi\DbBlade\Models\DbView', 'table_name' => 'db_views', /** * The default name field used to look up for the model. * e.g. DbView::make($viewName) or dbview($viewName) */ 'name_field' => 'name', /** * The default model field to be compiled when not explicitly specified * with DbView::field($fieldName) or DbView::model($modelName, $fieldName) */ 'content_field' => 'content', /** * This property will be added to models being compiled with DbView * to keep track of which field in the model is being compiled */ 'model_property' => '__db_blade_compiler_content_field', 'cache' => false, 'cache_path' => 'app/db-blade/cache/views' ];
配置完成后并确保迁移已发布,您可以运行迁移来创建数据库视图表
php artisan migrate
使用方法
此包提供了一个与 View
完全相同的语法和功能的 DbView
门面
return DbView::make('home')->with(['foo' => 'bar']);
您还可以使用与 Laravel 基础 view()
助手功能相匹配的 dbview()
助手。如果没有提供任何参数,将返回工厂
return dbview()->make('home')->with(['foo' => 'bar']);
如果提供了一个字符串,将查找并渲染具有该名称的视图
return dbview('home')->with(['foo' => 'bar']);
运行时覆盖设置
您可以通过关联方法使用关联方法覆盖模型、名称字段和内容字段的单个设置
return DbView::make('home')->model('App\Template'); return DbView::make('home')->field('template_name'); // You can also pass the model and name field as a shorthand: return DbView::make('home')->model('App\Template', 'template_name'); // Override content field return DbView::make('home')->contentField('template_content'); // ... or a combination of these return DbView::make('home')->model('App\Template', 'template_name')->contentField('template_content');
缓存
默认情况下,配置文件中禁用了缓存。如果您启用此设置,则将在所需路径中存储视图的编译版本。如果模型已更新,则将 updated_at 字段与文件修改日期进行比较,并重新渲染和缓存视图。
变更日志
有关最近更改的更多信息,请参阅 CHANGELOG。
待办事项列表
- 公开视图查找回调
- 将标准视图组合器与 dbview 组合器分开。
- 单元测试
许可证
laravel-db-blade 是开源软件,根据 MIT 许可证(MIT)许可。有关更多信息,请参阅 许可证文件。