在Laravel 5.4预览Mailables当浏览器发生错误

问题描述 投票:3回答:1

我试图预览Mailables的浏览器,但我得到这个错误。

Object of class App\Mail\ExamNotification could not be converted to string

我跟着https://laravel.com/docs/master/mail所有指令并不能发现什么导致此错误。

这里是我的路由文件

Route::get('/mailable', function () {
    $parent = App\Parents::find(2);

    return new App\Mail\ExamNotification($parent);
});

这里是软件\邮件\ ExamNotification.php文件的内容

<?php

namespace App\Mail;

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

use App\Parents;

class ExamNotification extends Mailable
{
    use Queueable, SerializesModels;

    /**
     * The parent instance.
     *
     * @var Parent
     */
    public $parent;

    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct(Parents $parent)
    {
        $this->parent = $parent;
    }


    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->from('[email protected]')
                    ->markdown('emails.exams.notification');
    }
}

这里是查看文件的内容

@component('mail::message')
# Introduction
Parent Name is {{ $parent->name }}
The body of your message.

@component('mail::button', ['url' => ''])
Button Text
@endcomponent

Thanks,<br>
{{ config('app.name') }}
@endcomponent

而且请注意,当I DD($这个 - >父)在构造函数中我得到了正确的查询对象。但我无法DD($这个 - >父);构建函数内(上述即,相同的错误发生)。

编辑:请注意,我还能够发送使用此功能的邮件我的路线文件中

Mail::to('[email protected]')->send(new App\Mail\ExamNotification($parent));

所以也能够

dd(new App\Mail\ExamNotification($parent)); 

而没有任何问题的上面的输出是

    ExamNotification {#664 ▼
  +parent: Parents {#687 ▶}
  +from: []
  +to: []
  +cc: []
  +bcc: []
  +replyTo: []
  +subject: null
  #markdown: null
  +view: null
  +textView: null
  +viewData: []
  +attachments: []
  +rawAttachments: []
  +callbacks: []
  +connection: null
  +queue: null
  +delay: null
}

和DD($这个 - >亲本)的输出;内部构造是

    Parents {#687 ▼
  #guard: "parent"
  #table: "parents"
  #dates: array:1 [▶]
  #fillable: array:7 [▶]
  #hidden: array:2 [▶]
  #connection: "mysql"
  #primaryKey: "id"
  #keyType: "int"
  +incrementing: true
  #with: []
  #withCount: []
  #perPage: 15
  +exists: true
  +wasRecentlyCreated: false
  #attributes: array:15 [▼
    "id" => 2
    "name" => "James Kurian"
  ]
  #original: array:15 [▶]
  #casts: []
  #dateFormat: null
  #appends: []
  #events: []
  #observables: []
  #relations: []
  #touches: []
  +timestamps: true
  #visible: []
  #guarded: array:1 [▶]
  #forceDeleting: false
  #rememberTokenName: "remember_token"
  #forceDeleting: false
}

所以这是非常清楚的,这个问题只与预览邮件和未发送邮件。

请告诉我在哪里,我犯这样的错误,并在此先感谢。

php laravel
1个回答
6
投票

你有这个问题的原因是因为渲染Mailables的能力在Laravel 5.5才被介绍。

您应该能够通过添加到您ExamNotification虽然自己实现的功能:

public function render()
{
    $this->build();

    if ($this->markdown) {
        return $this->buildMarkdownView()['html'];
    }

     return view($this->buildView(), $this->buildViewData());
}

为此,您需要更新您的路由调用render()方法:

Route::get('/mailable', function () {
    $parent = App\Parents::find(2);

    return (new App\Mail\ExamNotification($parent))->render();
});

我只用一对夫妇的情况下测试这一点,但它似乎很好地工作。

© www.soinside.com 2019 - 2024. All rights reserved.