このページの2つのバージョン間の差分を表示します。
|
プログラム言語:php:laravel:dbに保存した内容をテンプレートとして表示 [2022/07/22 17:29] humolife 作成 |
プログラム言語:php:laravel:dbに保存した内容をテンプレートとして表示 [2022/07/22 18:02] (現在) humolife |
||
|---|---|---|---|
| 行 1: | 行 1: | ||
| ====== 【Laravel】DBに保存した内容をテンプレートとして表示 ====== | ====== 【Laravel】DBに保存した内容をテンプレートとして表示 ====== | ||
| - | [[https:// | + | string-blade-compiler を使うと簡単にできる。\\ |
| + | * [[https:// | ||
| + | DBに保存した内容を view として使いたい場合は、上記ドキュメントを参照。 | ||
| ===== インストール ===== | ===== インストール ===== | ||
| + | < | ||
| + | composer require " | ||
| + | </ | ||
| + | |||
| + | ===== DBに保存した内容をメールのテンプレートとして使いたい場合 ===== | ||
| + | メールテンプレートを保存しているテーブル設計は以下とする。 | ||
| + | < | ||
| + | CREATE TABLE `mail_templates` ( | ||
| + | `id` bigint unsigned NOT NULL AUTO_INCREMENT, | ||
| + | `sort` tinyint unsigned NOT NULL COMMENT ' | ||
| + | `type` enum(' | ||
| + | `template` text COLLATE utf8mb4_unicode_ci NOT NULL, | ||
| + | `created_at` timestamp NULL DEFAULT NULL, | ||
| + | `updated_at` timestamp NULL DEFAULT NULL, | ||
| + | PRIMARY KEY (`id`), | ||
| + | UNIQUE KEY `mail_templates_sort_type_unique` (`sort`, | ||
| + | ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci | ||
| + | </ | ||
| + | 以下「TestMail.php」について、特筆すべき点が無い部分は省略。\\ | ||
| + | (省略部分は元々の記載をそのまま使えば問題ないです) | ||
| + | < | ||
| + | class TestMail extends Mailable | ||
| + | { | ||
| + | public $text_template; | ||
| + | public $html_template; | ||
| + | public $user; | ||
| + | |||
| + | public function __construct($user) | ||
| + | { | ||
| + | $this-> | ||
| + | } | ||
| + | |||
| + | public function build() | ||
| + | { | ||
| + | // sort 1 がフッターとする | ||
| + | $mail_footers = MailTemplate:: | ||
| + | -> | ||
| + | -> | ||
| + | |||
| + | // sort 2 がメールのテンプレートとする | ||
| + | $mail_templates = MailTemplate:: | ||
| + | -> | ||
| + | -> | ||
| + | |||
| + | if (is_null($mail_templates)) { | ||
| + | return; | ||
| + | } | ||
| + | |||
| + | if (isset($mail_templates[' | ||
| + | $this-> | ||
| + | ' | ||
| + | ], [ | ||
| + | ' | ||
| + | ' | ||
| + | ])-> | ||
| + | |||
| + | $this-> | ||
| + | } | ||
| + | |||
| + | if (isset($mail_templates[' | ||
| + | $this-> | ||
| + | ' | ||
| + | ], [ | ||
| + | ' | ||
| + | ' | ||
| + | ])-> | ||
| + | |||
| + | $this-> | ||
| + | } | ||
| + | |||
| + | return $this-> | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | ※ 説明を簡潔にするため、上記コード内で sort の数字を直接記載しているが、本来は定数を使う | ||
| + | < | ||
| + | {!! $text_template !!} | ||
| + | </ | ||
| + | < | ||
| + | {{ $html_template }} | ||
| + | </ | ||