moh_abk
moh_abk

Reputation: 2164

Passing data to Laravel event

I've just started to read about events in laravel and have been stuck for a few hours now. This may be very easy but I'm missing something.

I fire the event like this

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

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

As you can see I'm passing an eloquent object to the event.

I create the event;

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

I create the listener

<?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('[email protected]', 'xxx Restaurant Team');
            $message->subject('Welcome to xxx');
            $message->to($referrer->email)->bcc('[email protected]');
        });
    }
}

This is where I'm stuck, how can I access the customer I passed to the event and access it's attributes like email, first_name etc I also need to pass it to my email template to gain access to those attributes to personalise the email.

I'm getting an error on this line $referrer->email

field email not found in class

Any help appreciated

This is what I get from 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) "[email protected]"
    ["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) "[email protected]"
    ["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)
}

I just want to be able to access the attributes.

Upvotes: 5

Views: 8000

Answers (1)

patricus
patricus

Reputation: 62368

This is not a code error. Your code should work fine. This is just an issue with your IDE. Because of the way field attributes work on Eloquent models, your IDE has no idea that the email field actually does exist, so it reports it as missing.

The barryvdh/laravel-ide-helper package may help with this issue.

In addition to the above package, providing a proper PHPDoc for your public $referrer; attribute may help, as well.

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

    // ...
}

Upvotes: 1

Related Questions