我正试图在laravel中的markdown邮件模板中使用php Trait方法,问题是我收到以下错误:
ErrorException : Call to undefined method Illuminate\View\Engines\CompilerEngine::getExcerpt() (View: C:\xampp\htdocs\dtcburger.com\resources\views\emails\weekly\weekly-mail-1.blade.php)
我正在尝试使用我的Strings php Trait类中的getExcerpt mehod,但它不起作用。
这是我的可邮寄课程:
<?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;
}
}
我的降价电子邮件模板:
@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
我的Strings Trait里面有方法getExcerpt:
<?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;
}
}
这是因为你的WeeklyMail类背景中不再存在。
我建议你只是做一个你的事件模型的访问器
use Strings;
function getExcerptBodyAttribute(){
return $this->getExcerpt($this->body, 0, 100)
}
然后在你的模板中
<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>
如果你的事件不是一个雄辩的模型,那么juste会做一个设置他的excerpt_body
的二传手