我试图在浏览器中预览Mailables但我收到此错误.
Object of class App\Mail\ExamNotification could not be converted to string
Run Code Online (Sandbox Code Playgroud)
我按照https://laravel.com/docs/master/mail中的所有说明操作,无法找到导致此错误的原因.
这是我的路线文件
Route::get('/mailable', function () {
$parent = App\Parents::find(2);
return new App\Mail\ExamNotification($parent);
});
Run Code Online (Sandbox Code Playgroud)
这是App\Mail\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('donot-replay@example.com')
->markdown('emails.exams.notification');
}
}
Run Code Online (Sandbox Code Playgroud)
这是视图文件的内容
@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
Run Code Online (Sandbox Code Playgroud)
请注意,当我在构造函数内部($ this-> parent)时,我得到了正确的查询对象.但我无法dd($ this-> parent); 内部构建函数(即上面提到的相同错误).
编辑:请注意我也可以使用此功能在我的路径文件中发送邮件
Mail::to('app@example.com')->send(new App\Mail\ExamNotification($parent));
Run Code Online (Sandbox Code Playgroud)
所以也能够
dd(new App\Mail\ExamNotification($parent));
Run Code Online (Sandbox Code Playgroud)
没有任何问题,上面的输出是
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
}
Run Code Online (Sandbox Code Playgroud)
和dd的输出($ this-> parent); 内部构造函数是
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
}
Run Code Online (Sandbox Code Playgroud)
所以很明显,问题只在于预览邮件而不是发送邮件.
请告诉我我在哪里弄错了,并提前感谢.
你遇到这个问题的原因是因为渲染Mailables的能力只在Laravel 5.5中引入.
你应该能够自己实现这个功能,只需添加到ExamNotification:
public function render()
{
$this->build();
if ($this->markdown) {
return $this->buildMarkdownView()['html'];
}
return view($this->buildView(), $this->buildViewData());
}
Run Code Online (Sandbox Code Playgroud)
您需要更新Route以调用render()方法:
Route::get('/mailable', function () {
$parent = App\Parents::find(2);
return (new App\Mail\ExamNotification($parent))->render();
});
Run Code Online (Sandbox Code Playgroud)
我只用几个场景对它进行了测试,但似乎工作正常.
| 归档时间: |
|
| 查看次数: |
504 次 |
| 最近记录: |