内容へ移動
ゆーものメモ帳
ユーザ用ツール
管理
ログイン
サイト用ツール
検索
ツール
文書の表示
以前のリビジョン
バックリンク
最近の変更
メディアマネージャー
サイトマップ
ログイン
>
最近の変更
メディアマネージャー
サイトマップ
トレース:
プログラム言語:php:laravel:dbに保存した内容をテンプレートとして表示
この文書は読取専用です。文書のソースを閲覧することは可能ですが、変更はできません。もし変更したい場合は管理者に連絡してください。
====== 【Laravel】DBに保存した内容をテンプレートとして表示 ====== string-blade-compiler を使うと簡単にできる。\\ * [[https://packagist.org/packages/wpb/string-blade-compiler|ドキュメント]] DBに保存した内容を view として使いたい場合は、上記ドキュメントを参照。 ===== インストール ===== <code:shell> composer require "wpb/string-blade-compiler" </code> ===== DBに保存した内容をメールのテンプレートとして使いたい場合 ===== メールテンプレートを保存しているテーブル設計は以下とする。 <code> CREATE TABLE `mail_templates` ( `id` bigint unsigned NOT NULL AUTO_INCREMENT, `sort` tinyint unsigned NOT NULL COMMENT '種類', `type` enum('text','html') COLLATE utf8mb4_unicode_ci NOT NULL COMMENT 'タイプ', `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`,`type`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci </code> 以下「TestMail.php」について、特筆すべき点が無い部分は省略。\\ (省略部分は元々の記載をそのまま使えば問題ないです) <code:php|app\Mail\TestMail.php> class TestMail extends Mailable { public $text_template; public $html_template; public $user; public function __construct($user) { $this->user= $user; } public function build() { // sort 1 がフッターとする $mail_footers = MailTemplate::where('sort', 1) ->pluck('template', 'type') ->toArray(); // sort 2 がメールのテンプレートとする $mail_templates = MailTemplate::where('sort', 2) ->pluck('template', 'type') ->toArray(); if (is_null($mail_templates)) { return; } if (isset($mail_templates['text'])) { $this->text_template = view([ 'template' => $mail_templates['text'], ], [ 'user' => $this->user, 'footer' => $mail_footers['text'] ?? '', ])->render(); $this->text('mail.text'); } if (isset($mail_templates['html'])) { $this->html_template = view([ 'template' => $mail_templates['html'], ], [ 'user' => $this->user, 'footer' => $mail_footers['html'] ?? '', ])->render(); $this->view('mail.html'); } return $this->subject('test'); } } </code> ※ 説明を簡潔にするため、上記コード内で sort の数字を直接記載しているが、本来は定数を使う <code:php|resources\views\mail\text.blade.php> {!! $text_template !!} </code> <code:php|resources\views\mail\html.blade.php> {{ $html_template }} </code>
プログラム言語/php/laravel/dbに保存した内容をテンプレートとして表示.txt
· 最終更新: 2022/07/22 18:02 by
humolife
ページ用ツール
文書の表示
以前のリビジョン
バックリンク
文書の先頭へ