duandou9931 2019-04-11 15:26
浏览 39

在降价电子邮件模板中使用特征问题

I'm trying to use php Trait method inside my markdown mail template in laravel, problem is I'm getting the following error:

 ErrorException  : Call to undefined method Illuminate\View\Engines\CompilerEngine::getExcerpt() (View: C:\xampp\htdocs\dtcburger.comesources\views\emails\weekly\weekly-mail-1.blade.php)

I'm trying to use getExcerpt mehod from my Strings php Trait class, but it's not working.

This is my mailable class:

<?php
namespace App\Mail\weekly;


use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
use App\Traits\Strings;


class WeeklyMail1 extends Mailable
{


    use Queueable, SerializesModels;
    use Strings;
    public $mailData;


    public function __construct($mailData)
    {
        $this->mailData = $mailData;
    }


    public function build()
    {
        $mail = $this->markdown('emails.weekly.weekly-mail-1')
        ->from(env('MAIL_FROM'), env('MAIL_FROM_NAME'))
        ->subject($this->mailData['subject']);

        return $mail;
    }


}

And my markdown mail template:


@component('mail::message', ['mailData' => $mailData])

# {{ $mailData['subject'] }}

@if(count($mailData['events']) > 0)
# No te pierdas nuestros próximos eventos
@component('mail::table')
|  |  |
| :------------- | :------------- |
@foreach($mailData['events'] as $event)
| <img src="{{ $event->image }}" style="min-width:120px; max-width:120px; min-height:100px; max-height:100px;"> | <strong><p>{{ $event->title }}</p></strong><p>{{!! $this->getExcerpt($event->body, 0, 100) !!}}</p> |
@endforeach
@endcomponent
@endif

@endcomponent

And my Strings Trait with method getExcerpt inside:

<?php

namespace App\Traits;

use Str;

trait Strings
{
    public function slugify($title){
        $cleanString = $this->cleanString($title);
        $slugified = Str::lower( Str::slug($title, '-') );
        return $slugified;
    }


    public function getExcerpt($str, $startPos = 0, $maxLength = 30, $end = '[...]') {
        if(strlen($str) > $maxLength) {
            $excerpt   = substr($str, $startPos, $maxLength - 6);
            $lastSpace = strrpos($excerpt, ' ');
            $excerpt   = substr($excerpt, 0, $lastSpace);
            $excerpt  .= $end;
        } else {
            $excerpt = $str;
        }

        return $excerpt;
    }

}
  • 写回答

1条回答 默认 最新

  • doucigua0449 2019-04-11 16:29
    关注

    It's because your are no more in your WeeklyMail class context.

    I suggest you just do an accessor to your event Model wich does

    
    use Strings;
    
    function getExcerptBodyAttribute(){
          return $this->getExcerpt($this->body, 0, 100)
    }
    

    And then in your template

    <img src="{{ $event->image }}" style="min-width:120px; max-width:120px; min-height:100px; max-height:100px;"> | <strong><p>{{ $event->title }}</p></strong><p>{{!! $event->excerpt_body !!}}</p>
    

    if your event is not an eloquent model, juste make a setter which set his excerpt_body

    评论

报告相同问题?

悬赏问题

  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配