|
| 1 | +# Laravel DB Blade |
| 2 | + |
| 3 | + |
| 4 | +## Render Blade templates from Eloquent Model Fields |
| 5 | + |
| 6 | +This package allows you to render Blade templates from a database model instead of files. It is based on [Flynsarmy](https://github.com/Flynsarmy/)'s [**laravel-db-blade-compiler**](https://github.com/Flynsarmy/laravel-db-blade-compiler). |
| 7 | + |
| 8 | + |
| 9 | +## Installation |
| 10 | + |
| 11 | +You can install the package via composer: |
| 12 | + |
| 13 | +``` bash |
| 14 | +composer require kiroushi/laravel-db-blade |
| 15 | +``` |
| 16 | + |
| 17 | +Publish the assets using artisan: |
| 18 | + |
| 19 | +```bash |
| 20 | +php artisan vendor:publish |
| 21 | +``` |
| 22 | + |
| 23 | +And then set up your DbView model in the `config/db-blade.php` configuration file. A default model (**Kiroushi\DbBlade\Models\DbView**) is included in the package: |
| 24 | + |
| 25 | +```php |
| 26 | +return [ |
| 27 | + |
| 28 | + 'model_name' => 'Kiroushi\DbBlade\Models\DbView', |
| 29 | + 'table_name' => 'db_views', |
| 30 | + |
| 31 | + /** |
| 32 | + * The default name field used to look up for the model. |
| 33 | + * e.g. DbView::make($viewName) or dbview($viewName) |
| 34 | + */ |
| 35 | + 'name_field' => 'name', |
| 36 | + |
| 37 | + /** |
| 38 | + * The default model field to be compiled when not explicitly specified |
| 39 | + * with DbView::field($fieldName) or DbView::model($modelName, $fieldName) |
| 40 | + */ |
| 41 | + 'content_field' => 'content', |
| 42 | + |
| 43 | + /** |
| 44 | + * This property will be added to models being compiled with DbView |
| 45 | + * to keep track of which field in the model is being compiled |
| 46 | + */ |
| 47 | + 'model_property' => '__db_blade_compiler_content_field', |
| 48 | + |
| 49 | + 'cache' => false, |
| 50 | + 'cache_path' => 'app/db-blade/cache/views' |
| 51 | + |
| 52 | +]; |
| 53 | +``` |
| 54 | + |
| 55 | +After the configuration and ensuring that the migration has been published, you can create the database views table by running the migrations: |
| 56 | + |
| 57 | +```bash |
| 58 | +php artisan migrate |
| 59 | +``` |
| 60 | + |
| 61 | +## Usage |
| 62 | + |
| 63 | +This package offers a `DbView` facade with the exact same syntax and functionality than `View`: |
| 64 | + |
| 65 | +```php |
| 66 | +return DbView::make('home')->with(['foo' => 'bar']); |
| 67 | +``` |
| 68 | + |
| 69 | +You can also use the `dbview()` helper matching Laravel's base `view()` helper functionality. If no arguments are supplied, the factory is returned: |
| 70 | + |
| 71 | +```php |
| 72 | +return dbview()->make('home')->with(['foo' => 'bar']); |
| 73 | +``` |
| 74 | + |
| 75 | +If a string is supplied, a view with that name will be looked up for and rendered: |
| 76 | + |
| 77 | +```php |
| 78 | +return dbview('home')->with(['foo' => 'bar']); |
| 79 | +``` |
| 80 | + |
| 81 | +### Overriding settings at runtime |
| 82 | + |
| 83 | +You can override individual settings for the model, name field and content field by using associated methods: |
| 84 | + |
| 85 | +```php |
| 86 | +return DbView::make('home')->model('App\Template'); |
| 87 | + |
| 88 | +return DbView::make('home')->field('template_name'); |
| 89 | + |
| 90 | +// You can also pass the model and name field as a shorthand: |
| 91 | +return DbView::make('home')->model('App\Template', 'template_name'); |
| 92 | + |
| 93 | +// Override content field |
| 94 | +return DbView::make('home')->contentField('template_content'); |
| 95 | + |
| 96 | +// ... or a combination of these |
| 97 | +return DbView::make('home')->model('App\Template', 'template_name')->contentField('template_content'); |
| 98 | +``` |
| 99 | + |
| 100 | +### Cache |
| 101 | + |
| 102 | +By default, cache is disabled in config file. If you enable the setting, a compiled version of the views will be stored at the desired path. If the model is updated, the *updated_at* field will be checked against the file modification date and the view will be re-rendered and cached. |
| 103 | + |
| 104 | +## Changelog |
| 105 | + |
| 106 | +Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently. |
| 107 | + |
| 108 | +## To do list |
| 109 | + |
| 110 | +- Expose view finder callback |
| 111 | +- Separate standard view composers from dbview composers. |
| 112 | +- Unit tests |
| 113 | + |
| 114 | +## License |
| 115 | + |
| 116 | +**laravel-db-blade** is open-sourced software licensed under the MIT License (MIT). Please see [License File](LICENSE.md) for more information. |
0 commit comments