【发布时间】:2016-05-29 13:58:48
【问题描述】:
我刚刚开始在 laravel 中阅读有关 events 的信息,现在已经卡了几个小时了。这可能很容易,但我错过了一些东西。
我像这样触发事件
// get the referrer
$referrer = Customer::where('promocode', $user->referral_code)->first();
// fire referral sign up event
Event::fire(new ReferralSignupEvent($referrer));
如您所见,我将 eloquent 对象传递给事件。
我创建事件;
<?php
namespace App\Events;
use App\Models\Customer;
class ReferralSignupEvent extends Event
{
public $referrer;
/**
* Create a new event instance.
*
* @return void
*/
public function __construct(Customer $referrer)
{
//
$this->referrer = $referrer;
}
}
我创建了监听器
<?php
namespace App\Listeners;
use App\Events\ReferralSignupEvent;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
class EmailReferralSignupConfirmation
{
/**
* Create the event listener.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Handle the event.
*
* @param ReferralSignupEvent $event
* @return void
*/
public function handle(ReferralSignupEvent $event)
{
//
$referrer = $event->referrer;
$mailer = app()->make('mailer');
$mailer->queue('emails.restaurant-welcome', ['referrer' => $referrer] , function ($message) use ($referrer) {
$message->from('support@xxxx.com', 'xxx Restaurant Team');
$message->subject('Welcome to xxx');
$message->to($referrer->email)->bcc('support@xxx.com');
});
}
}
这是我卡住的地方,我如何访问我传递给事件的customer 并访问它的属性,如email, first_name 等
我还需要将其传递给我的电子邮件模板以访问这些属性以个性化电子邮件。
我在这一行遇到错误$referrer->email
field email not found in class
任何帮助表示赞赏
这是我从dd($referrer)得到的
object(App\Models\Customer)#79 (23) {
["connection":protected]=>
string(3) "web"
["table":protected]=>
string(9) "customers"
["fillable":protected]=>
array(15) {
[0]=>
string(5) "email"
[1]=>
string(8) "password"
[2]=>
string(10) "first_name"
[3]=>
string(9) "last_name"
[4]=>
string(12) "mobile_phone"
[5]=>
string(13) "address_line1"
[6]=>
string(13) "address_line2"
[7]=>
string(13) "address_line3"
[8]=>
string(5) "token"
[9]=>
string(5) "vcode"
[10]=>
string(9) "promocode"
[11]=>
string(14) "referral_count"
[12]=>
string(8) "verified"
[13]=>
string(11) "location_id"
[14]=>
string(7) "city_id"
}
["primaryKey":protected]=>
string(2) "id"
["perPage":protected]=>
int(15)
["incrementing"]=>
bool(true)
["timestamps"]=>
bool(true)
["attributes":protected]=>
array(19) {
["id"]=>
int(3)
["first_name"]=>
string(8) "Abubakar"
["last_name"]=>
string(8) "Mohammed"
["email"]=>
string(21) "xxx@gmail.com"
["password"]=>
string(60) "$2y$10$22iIZu7lvzjiNjxN4c6g6Ov1NCBmfSypVQ4RoL20qL4M5YqvAz/vS"
["mobile_phone"]=>
string(11) "07427356289"
["address_line1"]=>
string(17) "25 Hamilton House"
["address_line2"]=>
string(8) "Lonsdale"
["address_line3"]=>
string(9) "Wolverton"
["city"]=>
NULL
["vcode"]=>
string(0) ""
["promocode"]=>
string(8) "XXXXXXXX"
["referral_count"]=>
int(0)
["token"]=>
string(60) "$2y$10$22iIZu7lvzjiNjxN4c6g6OqJ.Hzr67xYdJj34w4XXkW2e_ioVv1Si"
["verified"]=>
string(1) "1"
["created_at"]=>
string(19) "2016-01-29 00:05:52"
["updated_at"]=>
string(19) "2016-02-11 17:42:15"
["city_id"]=>
int(1)
["location_id"]=>
int(2)
}
["original":protected]=>
array(19) {
["id"]=>
int(3)
["first_name"]=>
string(8) "Abubakar"
["last_name"]=>
string(8) "Mohammed"
["email"]=>
string(21) "xxx@gmail.com"
["password"]=>
string(60) "$2y$10$22iIZu7lvzjiNjxN4c6g6Ov1NCBmfSypVQ4RoL20qL4M5YqvAz/vS"
["mobile_phone"]=>
string(11) "07427356289"
["address_line1"]=>
string(17) "25 Hamilton House"
["address_line2"]=>
string(8) "Lonsdale"
["address_line3"]=>
string(9) "Wolverton"
["city"]=>
NULL
["vcode"]=>
string(0) ""
["promocode"]=>
string(8) "XXXXXXXX"
["referral_count"]=>
int(0)
["token"]=>
string(60) "$2y$10$22iIZu7lvzjiNjxN4c6g6OqJ.Hzr67xYdJj34w4XXkW2e_ioVv1Si"
["verified"]=>
string(1) "1"
["created_at"]=>
string(19) "2016-01-29 00:05:52"
["updated_at"]=>
string(19) "2016-02-11 17:42:15"
["city_id"]=>
int(1)
["location_id"]=>
int(2)
}
["relations":protected]=>
array(0) {
}
["hidden":protected]=>
array(0) {
}
["visible":protected]=>
array(0) {
}
["appends":protected]=>
array(0) {
}
["guarded":protected]=>
array(1) {
[0]=>
string(1) "*"
}
["dates":protected]=>
array(0) {
}
["dateFormat":protected]=>
NULL
["casts":protected]=>
array(0) {
}
["touches":protected]=>
array(0) {
}
["observables":protected]=>
array(0) {
}
["with":protected]=>
array(0) {
}
["morphClass":protected]=>
NULL
["exists"]=>
bool(true)
["wasRecentlyCreated"]=>
bool(false)
}
我只是希望能够访问属性。
【问题讨论】:
-
可以在handle方法中
dd($referrer)吗? -
@JilsonThomas 我怀疑如果我这样做我能看到转储,因为它不是视图
-
如果您使用 Chrome 进行检查,您可以在网络选项卡中看到输出。
-
True.. 但由于我已经对流程进行了端到端编码,因此很难测试
-
视图没有什么特别之处,除了最佳实践外,您只能在视图中输出项目。你可以在任何地方
dd,它的工作原理是一样的。
标签: php laravel laravel-5 lumen