ユーザ用ツール

サイト用ツール


プログラム言語:php:laravel:dbに保存した内容をテンプレートとして表示

差分

このページの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://packagist.org/packages/wpb/string-blade-compiler|ドキュメント]]+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