1

I was try to make some email to send email acticvation code when someone register. so i make it an event called UserRegistered. here's my RegisterController that call the event

public function store(Request $request){

    $this->validate( $request,
        [
            'name' => 'required|min:3',
            'username' => 'required|unique:users|min:3',
            'email' => 'required|email|unique:users',
            'password' => 'required|confirmed|min:12'
        ]
    );

    $user = User::create([
        'name' => request('name'),
        'email' => request('email'),
        'username' => request('username'),
        'password' => bcrypt(request('password')),
        'token' => random(30),
    ]);

    event(new UserRegistered($user));

    return back()->with('success','Please check your email to active your account.');
}

On the UserRegistered i pass the user data like this:

 class UserRegistered
{
    use Dispatchable, InteractsWithSockets, SerializesModels;
    public $user;



       /**
         * Create a new event instance.
         *
         * @return void
         */
        public function __construct(User $user)
        {
            $this->user = $user;
        }

        /**
         * Get the channels the event should broadcast on.
         *
         * @return \Illuminate\Broadcasting\Channel|array
         */
        public function broadcastOn()
        {
            return new PrivateChannel('channel-name');
        }

and here's my listener:

class SendActivationCode
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     *
     * @param  UserRegistered  $event
     * @return void
     */
    public function handle(UserRegistered $event)
    {
        Mail::to($event)->send(new SendActivationMail($event));
    }
}

and here the SendActivationMail class:

class SendActivationMail extends Mailable
{
    use Queueable, SerializesModels;
    protected $user;
        /**
         * Create a new message instance.
         *
         * @return void
         */
        public function __construct(User $user)
        {
            $this->user = $user;
        }

        /**
         * Build the message.
         *
         * @return $this
         */
        public function build()
        {
            return $this->markdown('mails.user.activation')->with([
                'name' => $this->user->name,
                'token' => $this->user->token,
            ]);
         }
}

but i got this errors:

"Type error: Argument 1 passed to App\Mail\SendActivationMail::__construct() must be an instance of App\User, instance of App\Events\UserRegistered given, called in E:\laragon\www\blog\app\Listeners\SendActivationCode.php on line 33 ◀"

My activation.blade.php:

@component('mail::message')
# Activation Code for {{ config('app.name') }}

Hi {{ $user->name }} You are recieved this because you are registered at our site. To active your account please click the link bellow.

@component('mail::button', ['url' => route('verify').$user->token])
Active Account
@endcomponent

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

so what can make me get this errors and how to fix it?

Ying
  • 1,282
  • 4
  • 19
  • 34

2 Answers2

2

Looks like your SendActivatiionMail class doesn't extend Illuminate\Mail\Mailable. Try this in SendActivatiionMail.php:

    namespace App\Mail;

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

    class SendActivatiionMail extends Mailable
    {
        use Queueable, SerializesModels;
    ...

and then in your event listener:

use App\Mail\SendActivationMail;

public function handle(UserRegistered $event)
{
    Mail::to($event->user->email)->send(new SendActivationMail($event->user));
}
Andriy Lozynskiy
  • 2,444
  • 2
  • 17
  • 35
  • now i igot this errors "Undefined property: App\Events\UserRegistered::$email". i'm sure i'm already pass the User when registered, why i can't use $event->email? – Ying Dec 03 '17 at 09:40
  • Your event handler and email class has the same name `SendActivatiionCode` right? – Andriy Lozynskiy Dec 03 '17 at 09:47
  • yes, my stupid, i then rename it into SendActivationMail for mail. – Ying Dec 03 '17 at 09:50
  • sorry i edited the question since i change the class name. the sendactivatiion mail already extend mailable. – Ying Dec 03 '17 at 09:59
  • it give me an error like this: "Undefined variable: user (View: E:\laragon\www\blog\resources\views\mails\user\activation.blade.php)" – Ying Dec 03 '17 at 10:15
  • ups sorry i forgot the blade, wait i wil added to question – Ying Dec 03 '17 at 10:16
  • just pass `$user` variable to the blade in the `build` function of your mail class – Andriy Lozynskiy Dec 03 '17 at 10:19
0

Your SendActivatiionCode must extends the mailable Class.

<?php

use Illuminate\Mail\Mailable;

class SendActivatiionCode extends Mailable {
    ...
}
Rafik Tighilt
  • 2,071
  • 1
  • 15
  • 27
  • now i igot this errors "Undefined property: App\Events\UserRegistered::$email". i'm sure i'm already pass the User when registered, why i can't use $event->email? – Ying Dec 03 '17 at 09:40
  • Your event has a User. You can try to do `$event->user->email;` – Rafik Tighilt Dec 03 '17 at 09:44
  • it still give me an errors, maybe because my stupid coz i give both SendActivationCode on event and mail. the other is already have mailable extend. – Ying Dec 03 '17 at 09:51
  • sorry i edited the question since i change the class name. the sendactivatiion mail already extend mailable. – Ying Dec 03 '17 at 09:59