php – 将数据传递给Laravel事件

我刚刚开始阅读laravel中的事件,现在已经停留了几个小时.这可能很容易,但我遗漏了一些东西.

我像这样开火

// get the referrer
$referrer = Customer::where('promocode', $user->referral_code)->first();

// fire referral sign up event
Event::fire(new ReferralSignupEvent($referrer));

正如你所看到的,我正在向这个事件传递一个雄辩的对象.

我创造了这个活动;

<?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');
        });
    }
}

这就是我被困住的地方,我如何访问我传递给事件的客户并访问它的属性,如email,first_name等
我还需要将其传递给我的电子邮件模板以访问这些属性以个性化电子邮件.

我在这行$referrer->电子邮件中收到错误

在课堂上找不到现场电子邮件

任何帮助赞赏

这是我从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)
}

我只是想能够访问属性.

最佳答案 这不是代码错误.你的代码应该工作正常.这只是IDE的一个问题.由于字段属性在Eloquent模型上的工作方式,您的IDE不知道电子邮件字段确实存在,因此它将其报告为缺失.

barryvdh/laravel-ide-helper软件包可能有助于解决此问题.

除了上面的包之外,为你的public $referrer提供一个合适的PHPDoc;属性也可能有所帮助.

class ReferralSignupEvent extends Event
{
    /**
     * The customer referrer.
     *
     * @var \App\Models\Customer
     */
    public $referrer;

    // ...
}
点赞